백준 [14502] "연구소"

Kimbab1004·2024년 4월 15일
0

Algorithm

목록 보기
33/102

오답

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <queue>

using namespace std;
int n, m;
int map[10][10];
int result = 0;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int visited[100][100] = { 0 };
vector<pair<int, int>> safe_zone;
vector<pair<int, int>> imfact_zone;
vector<pair<int, int>> walls;

void searchimfactzone(int a, int b) {
    if (visited[a][b] == 1) {
        return;
    }
    priority_queue<pair<int, int>> q;
    q.push(make_pair(a, b));
    while (!q.empty()) {
        int x = q.top().first;
        int y = q.top().second;
        visited[x][y] = 1;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = dx[i] + x;
            int ny = dy[i] + y;
            if (0 <= nx && nx < n && 0 <= ny && ny < m && map[nx][ny] == 0 && visited[nx][ny] == 0 && map[nx][ny] !=1) {
                map[nx][ny] = 2;
                q.push(make_pair(nx, ny));
            }
        }
    }
}

void searchsafezone(int a, int b) {
    if (visited[a][b] == 1) {
        return;
    }
    priority_queue<pair<int, int>> q;
    q.push(make_pair(a, b));
    while (!q.empty()) {
        int x = q.top().first;
        int y = q.top().second;
        visited[x][y] = 1;
        q.pop();
        if (map[x][y] == 0) {
            cout << x << " " << y << "구역 비감염 구역" << endl;
            result++;
        }
        for (int i = 0; i < 4; i++) {
            int nx = dx[i] + x;
            int ny = dy[i] + y;
            if (0 <= nx && nx < n && 0 <= ny && ny < m && map[nx][ny] == 0 && visited[nx][ny] == 0 &&  map[nx][ny] != 1) {
                q.push(make_pair(nx, ny));
            }
        }
    }
}

void input() {

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int a;
            cin >> a;
            if (a == 0) {
                safe_zone.push_back(make_pair(i, j));
            }
            else if (a == 1) {
                walls.push_back(make_pair(i, j));
            }
            else if (a == 2) {
                imfact_zone.push_back(make_pair(i, j));
            }
        }
    }
}

void bfs() {
    for (int i = 0; i < imfact_zone.size(); i++) {
        int x = imfact_zone[i].first;
        int y = imfact_zone[i].second;
        searchimfactzone(x, y);
    }
    memset(visited, 0, size(visited));
    for (int i = 0; i < safe_zone.size(); i++) {
        int x = safe_zone[i].first;
        int y = safe_zone[i].second;
        searchsafezone(x, y);   
    }
}

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

    input();
    bfs();

    cout << result;

    return 0;

}

0개의 댓글