알고리즘/백준

[백준 14502] 연구실 C++

겜도리도리 2021. 9. 11. 23:47
반응형

문제

백준 14502

 

14502번: 연구소

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

www.acmicpc.net

풀이

8x8의 작은 배열이기 때문에 가능한 모든 경우의 수에 대해 벽을 만들어 주는 브루트포스 방법을 사용합니다.

소스 코드

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
 
int n, m;
int premap[8][8];
int nextmap[8][8];
int cnt;
int dx[] = { 00-11 };
int dy[] = { -1100 };
int safefield;
int maxsafe;
bool check[64];
bool visited[8][8];
 
void copymap()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            nextmap[i][j] = premap[i][j];
        }
    }
}
 
void bfs(int sx, int sy)
{
    if (visited[sy][sx])
        return;
    visited[sy][sx] = true;
    queue<pair<intint>> q;
    queue<pair<intint>> virusq;
    q.push(make_pair(sx, sy));
    if (nextmap[sy][sx] == 0)
        virusq.push(make_pair(sx, sy));
    bool isvirus = false;
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (!isvirus)
            if (nextmap[y][x] == 2)
                isvirus = true;
        if (nextmap[y][x] != 1)
        {
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n)
                {
                    if (!visited[ny][nx] && nextmap[ny][nx] != 1)
                    {
                        visited[ny][nx] = true;
                        if (nextmap[ny][nx] == 0)
                            virusq.push(make_pair(nx, ny));
                        q.push(make_pair(nx, ny));
                    }
                }
            }
        }
    }
    // 바이러스 있으면 확산
    if (isvirus)
    {
        while (!virusq.empty())
        {
            int x = virusq.front().first;
            int y = virusq.front().second;
            virusq.pop();
            nextmap[y][x] = 2;
        }
    }
}
 
void wall()
{
    if (cnt == 3)
    {
        memset(visited, 0sizeof(visited));
        copymap();
        safefield = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                bfs(j, i);
            }
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (nextmap[i][j] == 0)
                    safefield++;
            }
        }
        maxsafe = max(maxsafe, safefield);
        return;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (premap[i][j] == 0)
                {
                    premap[i][j] = 1;
                    cnt++;
                    wall();
                    cnt--;
                    premap[i][j] = 0;
                }
            }
        }
    }
}
 
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> premap[i][j];
        }
    }
    wall();
    cout << maxsafe;
    return 0;
}
cs
반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준 1915] 가장 큰 정사각형 C++  (0) 2021.09.13
[백준 3190] 뱀 C++  (0) 2021.09.12
[백준 2583] 영역 구하기 C++  (0) 2021.09.10
[백준 1992] 쿼드트리 (C++)  (0) 2021.09.09
[백준 1759] 암호 만들기 (C++)  (0) 2021.09.08