본문 바로가기
백준

백준 14502 연구소

by 콩순이냉장고 2021. 2. 17.

문제 URL : www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크

www.acmicpc.net

문제 접근법:

벽3개를 설치하는것은  모든 경우의 수를 조사합니다. 즉 백트래킹으로 모든경우의 수를 다구합니다.

3개를 설치하고 나면 바이러스는 BFS 로 퍼트리고 안전영역을 구하면 되기때문에

어렵지 않은 문제입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//By 콩순이냉장고
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int n, m;
int map[8][8];
int dy[4= { -1,0,1,0 };
int dx[4= { 0,1,0,-1 };
 
int maxcount;
int board[8][8];
bool isrange(int y, int x) {
    return 0 <= y && y < n && 0 <= x && x < m;
}
 
int bfs() {
    bool visit[8][8= { 0 };
    memcpy(board, map, sizeof(map));//벽3개를 설치했던 맵을 board에 copy해줌
    queue<pair<intint>> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (board[i][j] == 2)//바이러스를 찾음
                q.push({ i,j });
        }
    }
    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (isrange(ny, nx) && board[ny][nx] == 0 && visit[ny][nx] == 0) {
                q.push({ ny,nx });
                board[ny][nx] = 2;
                visit[ny][nx] = 1;
            }
        }
    }
 
    //
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cnt += !board[i][j];
        }
    }
    return cnt;
}
 
void makewall(int y=0int x=0,int cnt=0) {
    if (cnt >= 3) {
        maxcount = max(maxcount, bfs());
        return;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (map[i][j] == 0) {
                map[i][j] = 1;
                makewall(i, j, cnt + 1);
                map[i][j] = 0;
            }
        }
    }
}
void input() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> map[i][j];
        }
    }
}
void solve() {
    makewall();
    cout << maxcount << "\n";
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    input();
    solve();
}
cs

 

모르는점 혹은 궁금한전 혹은 다른아이디어 어떤 댓글이든 환영입니다.

 

'백준' 카테고리의 다른 글

백준 2931 가스관  (0) 2021.04.05
백준 14888 연산자 끼워넣기  (0) 2021.02.17
백준 2568 전깃줄 - 2  (0) 2021.02.15
백준 3745 오름세  (0) 2021.02.15
백준 9370 미확인 도착지  (0) 2021.02.15