문제 출처 : https://www.acmicpc.net/problem/14502
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다. 일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다.
www.acmicpc.net
1) 브루트 포스->벽을 세울 수 있는 모든 경우 구하기
2) bfs-> 바이러스가 퍼진 후 안전 구역의 개수 구하기
<c++>
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
|
#include<iostream>
#include<queue>
#include<string.h>
#define MAX 10
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int d[MAX][MAX];
int count=0;
int n,m;
bool v[MAX][MAX];
queue<pair<int,int> > q;
int ans[MAX][MAX];
int maxs=0;
void bfs()
{
memset(v,false,sizeof(v));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
ans[i][j]=d[i][j];
if(d[i][j]==2)
{
q.push({i,j});
//v[i][j]=true;
}
}
}
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int t=0;t<4;t++)
{
int nx=x+dx[t];
int ny=y+dy[t];
if(nx<0||nx>=n||ny<0||ny>=m) continue;
if(ans[nx][ny]==0)
{
q.push({nx,ny});
ans[nx][ny]=2;
}
}
}
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ans[i][j]==0)
{
cnt++;
}
}
}
if(maxs<cnt) maxs=cnt;
}
void dfs(int cnt)
{
if(cnt==3)
{
bfs();
return ;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(d[i][j]==0)
{
d[i][j]=1;
dfs(cnt+1);
d[i][j]=0;
}
}
}
}
int main(void)
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>d[i][j];
if(d[i][j]==2)
{
q.push({i,j});
}
}
}
dfs(0);
cout<<maxs<<endl;
return 0;
}
|
cs |
'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글
16928번_뱀과 사다리 게임 (0) | 2020.02.19 |
---|---|
16948번_데스 나이트 (0) | 2020.02.19 |
12886번_돌 그룹 (0) | 2020.02.19 |
16933번_벽 부수고 이동하기 3 (0) | 2020.02.18 |
14442번_벽 부수고 이동하기 2 (0) | 2020.02.18 |