๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 2234๋ฒˆ ์„ฑ๊ณฝ

yulee_to 2022. 7. 16. 20:06

๋ฐฑ์ค€
2234๋ฒˆ ์„ฑ๊ณฝ
2234๋ฒˆ ์„ฑ๊ณฝ


๐Ÿค”๋ฌธ์ œ ์ดํ•ด

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” 1,2,3๋ฒˆ์„ ๊ณ„์‚ฐํ•˜๋˜ ๋ฒฝ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ 1, 2, 4, 8๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.


 

๐Ÿ”ฅํ’€์ด๐Ÿ”ฅ

๊ฐ„๋‹จํ•œ BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜๋˜ ๋ฒฝ์˜ ์ •๋ณด๋ฅผ ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

((1 << i) & map[y][x]) == 0)๋Š” BFS ํ•จ์ˆ˜์—์„œ ์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ,์œ„,์•„๋ž˜๋กœ ์ด๋™ํ•˜๊ธฐ ์ „์— ๊ทธ ๋ฐฉํ–ฅ์— ๋ฒฝ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

3๋ฒˆ์„ ๊ณ„์‚ฐํ•  ๋•Œ์—๋Š” map[i][j] = map[i][j] & ~(1 << k)์‹์„ ์ด์šฉํ•ด ๋ฒฝ์„ ํ•˜๋‚˜์”ฉ ์—†์•ค ์ •๋ณด๋ฅผ map์— ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ฃผ๊ณ  BFS๋ฅผ ๋Œ๋ฆฐ ํ›„ map๊ฐ’์„ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋Œ๋ ค์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

 

โŒํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

BFS๋ฅผ ๋Œ๊ธฐ ์ „ ์‹œ์ž‘์ ์„ visited์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๊นœ๋นกํ•ด ์˜ค๋ฅ˜๋ฅผ ์ฐพ๋Š”๊ฒŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

์ปดํŒŒ์ผ ์—๋Ÿฌ์˜ ๊ฒฝ์šฐ memset ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ <cstring>ํ—ค๋”๋ฅผ ๋นผ๋จน์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int n, m;
int map[51][51];
bool visited[51][51];

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

queue<pair<int, int>> q;
int max_room;
int remove_max_room;
int cnt;
int room_size;

bool isValid(int y, int x) {
    return y < m && y >= 0 && x < n && x >= 0 && !visited[y][x];
}

void BFS() {
    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        room_size++;

        for (int i = 0; i < 4; i++) {
            if (((1 << i) & map[y][x]) == 0) {
                int mv_y = y + dy[i];
                int mv_x = x + dx[i];
                if (isValid(mv_y, mv_x)) {
                    q.push({mv_y, mv_x});
                    visited[mv_y][mv_x] = true;
                }
            }
        }
    }
}

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

    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
        }
    }

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (!visited[i][j]) {
                visited[i][j] = true;
                cnt++;
                q.push({i, j});
                room_size = 0;
                BFS();
                if (max_room < room_size) max_room = room_size;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < 4; k++) {
                memset(visited, false, sizeof(visited));
                int tmp = map[i][j];
                map[i][j] = map[i][j] & ~(1 << k);
                q.push({i, j});
                room_size = 0;
                visited[i][j] = true;
                BFS();
                remove_max_room = remove_max_room < room_size ? room_size : remove_max_room;
                map[i][j] = tmp;
            }
        }
    }
    cout << cnt << "\n" << max_room << "\n" << remove_max_room;
}
728x90