[C++] ๋น„ํŠธ๋งˆ์Šคํฌ(BitMask)

2020. 5. 18. 13:30ยท๐Ÿ“ Computer Science/โœ Algorithm

๋น„ํŠธ๋งˆ์Šคํฌ(BitMask)

์ง‘ํ•ฉ ์š”์†Œ๋“ค์˜ ๊ตฌ์„ฑ ์—ฌ๋ถ€๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์œ ์šฉํ•œ ํ…Œํฌ๋‹‰์ด๋‹ค.

 

๊ธฐ๋ณธ ์›๋ฆฌ

{1, 2, 3, 4, 5}๋ผ๋Š” ์ง‘ํ•ฉ์ด ์žˆ๊ณ  ์ด ์ง‘ํ•ฉ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

{1}, {2}, ..., {1, 2}, ..., {1, 2, 5}, ..., {1, 2, 3, 4, 5}

 

๋ฌผ๋ก  ๊ฐ„๋‹จํžˆ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋น„ํŠธ ๋งˆ์Šคํ‚น์„ ํ•˜๋ฉด ๊ฐ ์š”์†Œ๋ฅผ ์ธ๋ฑ์Šค์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•˜์—ฌ ํšจ์œจ์ ์ธ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

{1, 2, 3, 4, 5} → 11111
{2, 3, 4, 5} → 11110
{1, 2, 5} → 10011
{2} → 00010

 

1) ์‚ฝ์ž…

i๋ฒˆ ์งธ ๋น„ํŠธ ๊ฐ’์„ 1๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ OR ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•œ๋‹ค.

i = 3์ผ ๋•Œ, 10101 | (1 << 3) → 10101 | 01000 → 11101

 

2) ์‚ญ์ œ

i๋ฒˆ ์งธ ๋น„ํŠธ ๊ฐ’์„ 0์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ AND ์—ฐ์‚ฐ๊ณผ NOT ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•œ๋‹ค.

i = 3์ผ ๋•Œ, 11101 & (~1 << 3) → 11101 & 10111 → 10101

 

3) ์กฐํšŒ

i๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ ๋ฌด์Šจ ๊ฐ’์ธ์ง€ ์•Œ๋ ค๋ฉด AND ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•œ๋‹ค.

10101 & (1 << i) → 0์ด๋ฉด i๋ฒˆ์งธ ๋น„ํŠธ ๊ฐ’์ด 0์ด๋‹ค.

 

๋ฌธ์ œ

 

https://www.acmicpc.net/problem/2098

 

2098๋ฒˆ: ์™ธํŒ์› ์ˆœํšŒ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 16) ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋น„์šฉ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ–‰๋ ฌ์˜ ์„ฑ๋ถ„์€ 1,000,000 ์ดํ•˜์˜ ์–‘์˜ ์ •์ˆ˜์ด๋ฉฐ, ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š” 0์ด ์ฃผ์–ด์ง„๋‹ค. W[i][j]๋Š” ๋„์‹œ i์—์„œ j

www.acmicpc.net

#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1e9;

int N;
int W[16][16];
int dp[16][1 << 16];

int DFS(int cur, int visited)
{
	int& ret = dp[cur][visited];
	if (ret > 0)
		return ret;

	if (visited == (1 << N) - 1) { // ๋ชจ๋“  ๋„์‹œ๋ฅผ ๋‹ค ๋ฐฉ๋ฌธํ•จ
		// ์ˆœํšŒ: ๋‹ค์‹œ 0๋ฒˆ์งธ ๋„์‹œ๋กœ ๋Œ์•„๊ฐ„๋‹ค.
		if (W[cur][0] != 0)
			return W[cur][0];
		return INF;
	}

	ret = INF;
	for (int i = 0; i < N; i++)
		if (W[cur][i] && !(visited & (1 << i)))
			ret = min(ret, W[cur][i] + DFS(i, visited | (1 << i)));
	return ret;
}

int main()
{
	cin >> N;
	for (int y = 0; y < N; y++)
		for (int x = 0; x < N; x++)
			cin >> W[y][x];
	cout << DFS(0, 1);
	return 0;
}

 

https://www.acmicpc.net/problem/1194

 

1194๋ฒˆ: ๋‹ฌ์ด ์ฐจ์˜ค๋ฅธ๋‹ค, ๊ฐ€์ž.

์ฒซ์งธ ์ค„์— ๋ฏธ๋กœ์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 50) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๋ฏธ๋กœ์˜ ๋ชจ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ™์€ ํƒ€์ž…์˜ ์—ด์‡ ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ์ˆ˜ ์žˆ๊ณ , ๋ฌธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ ,

www.acmicpc.net

#include <iostream> 
#include <algorithm>
#include <queue>
using namespace std;

int N, M;
char map[50][50];
bool visited[50][50][1 << 6];
int dir[4][2]{ {0,-1},{0,1},{-1,0},{1,0} };

typedef struct {
	int x, y, cnt, key;
}Point;

int bfs(Point start)
{
	queue<Point> q;
	q.push(start);
	visited[start.x][start.y][start.key] = true;

	while (!q.empty()) {
		Point cur = q.front();
		q.pop();

		for (int i = 0; i < 4; i++) {
			int nx = cur.x + dir[i][0], ny = cur.y + dir[i][1];
			if (nx < 0 || nx >= N || ny < 0 || ny >= M || map[nx][ny] == '#' || visited[nx][ny][cur.key])
				continue;

			if (map[nx][ny] == '1')	// ์ถœ๊ตฌ
				return cur.cnt + 1;
			else if (map[nx][ny] == '.') {
				q.push({ nx, ny, cur.cnt + 1, cur.key });
				visited[nx][ny][cur.key] = true;
			}
			else if ('a' <= map[nx][ny] && map[nx][ny] <= 'f') {	// ์—ด์‡ 
				int key = 1 << (map[nx][ny] - 'a');
				q.push({ nx, ny, cur.cnt + 1, cur.key | key });
				visited[nx][ny][cur.key | key] = true;
			}
			else if ('A' <= map[nx][ny] && map[nx][ny] <= 'F') {	// ๋ฌธ
				int door = 1 << (map[nx][ny] - 'A');
				if (cur.key & door)
				{
					q.push({ nx, ny, cur.cnt + 1, cur.key });
					visited[nx][ny][cur.key] = true;
				}
			}
		}
	}

	return -1;
}

int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(false);

	cin >> N >> M;

	Point start{};
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> map[i][j];
			if (map[i][j] == '0') {
				start = { i, j, 0, 0 };
				map[i][j] = '.';
			}
		}
	}

	cout << bfs(start);
}

 

https://www.acmicpc.net/problem/1102

 

1102๋ฒˆ: ๋ฐœ์ „์†Œ

์ฒซ์งธ ์ค„์— ๋ฐœ์ „์†Œ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 16๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ฐœ์ „์†Œ i๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐœ์ „์†Œ j๋ฅผ ์žฌ์‹œ์ž‘ํ•  ๋•Œ ๋“œ๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์ง„๋‹ค. i์ค„์˜ j๋ฒˆ์งธ ๊ฐ’์ด ๊ทธ

www.acmicpc.net

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int N, P;
int machine[16][16];
int dp[1 << 16];

int GetOnCount(int n)
{
    if (n == 0)
        return 0;
    return GetOnCount(n >> 1) + (n & 1);
}

int GetMinCost(int state)
{
    int& result = dp[state];

    if (result != -1)
        return result;
    if (GetOnCount(state) >= P)
        return 0;

    result = 1e9;

    for (int i = 0; i < N; i++)
        if ((state & (1 << i))) { // ์ž‘๋™ ์ค‘์ธ ๋ฐœ์ „๊ธฐ(on)
            for (int j = 0; j < N; j++) {
                if ((state & (1 << j)) == 0) { // ๊ณ ์žฅ๋‚œ ๋ฐœ์ „๊ธฐ(off)
                    // on ๋ฐœ์ „๊ธฐ๊ฐ€ off ๋ฐœ์ „๊ธฐ ์ž‘๋™
                    result = min(result, machine[i][j] + GetMinCost(state | (1 << j)));
                }
            }
        }

    return result;
}

int main()
{
    string onoff;

    cin >> N;
    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j)
            cin >> machine[i][j];
    cin >> onoff >> P;

    int on = 0; // ํ˜„์žฌ ์ž‘๋™ ์ค‘์ธ ๋ฐœ์ „๊ธฐ ๋„˜๋ฒ„ ๋น„ํŠธ ์ €์žฅ
    for (int i = 0; i < N; i++)
        if (onoff[i] == 'Y')
            on |= (1 << i); // i๋ฒˆ์งธ ๋น„ํŠธ์— 1(on) ์‚ฝ์ž…

    if (GetOnCount(on) >= P)
    {
        cout << 0;
        return 0;
    }

    memset(dp, -1, sizeof(dp));
    int result = GetMinCost(on);
    if (result == 1e9)
        cout << -1;
    else
        cout << result;
}

 

https://programmers.co.kr/learn/courses/30/lessons/92343

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–‘๊ณผ ๋Š‘๋Œ€

[0,0,1,1,1,0,1,0,1,0,1,1] [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]] 5 [0,1,0,1,1,0,1,0,0,1,0] [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6],[3,7],[4,8],[6,9],[9,10]] 5

programmers.co.kr

#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;

#define MAX_STATE 17

struct Point {
    int index, state, ship, wolf;
};

vector<int> graph[MAX_STATE];
bool visited[MAX_STATE][1 << MAX_STATE];

int solution(vector<int> info, vector<vector<int>> edges)
{
    int answer = 0;

    for (auto edge : edges) {
        graph[edge[0]].push_back(edge[1]);
        graph[edge[1]].push_back(edge[0]);
    }

    queue<Point> q;
    q.push({ 0,1,1,0 });
    visited[0][1] = true;
    while (!q.empty()) {
        Point curr = q.front();
        q.pop();
        answer = max(answer, curr.ship);

        for (int next : graph[curr.index]) {
            int nstate = curr.state | (1 << next);
            if (visited[next][nstate])
                continue;

            int ns = 0, nw = 0;
            if (!(curr.state & (1 << next))) {
                if (info[next]) { // ๋Š‘๋Œ€
                    if (curr.ship <= curr.wolf + 1)
                        continue;
                    nw = 1;
                }
                else { // ์–‘
                    ns = 1;
                }
            }

            visited[next][nstate] = true;
            q.push({ next,nstate,curr.ship + ns,curr.wolf + nw });
        }
    }

    return answer;
}

 

https://leetcode.com/problems/single-number-iii/

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        long xor_all = 0;
    
        for (int num : nums)
            xor_all ^= num;
        
        long diff_bit = xor_all & -xor_all;
        int num1 = 0, num2 = 0;
        
        for (int num : nums) {
            if (num & diff_bit)
                num1 ^= num;
            else
                num2 ^= num;
        }

        return {num1, num2};        
    }
};
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(ํฌ๋ฃจ์Šค์นผ)
  • [C++] ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ(Segment Tree)
  • [C++] ํŠธ๋ผ์ด(Trie)
  • [C++] ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ(Union-Find)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ๋น„ํŠธ๋งˆ์Šคํฌ(BitMask)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”