-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2638.cpp
More file actions
110 lines (101 loc) · 1.55 KB
/
2638.cpp
File metadata and controls
110 lines (101 loc) · 1.55 KB
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
// 2638. 치즈
// 2019.06.24
// BFS
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int map[101][101];
int visit[101][101];
int dist[101][101];
int ans;
int n, m;
int dx[4] = { 0,-1,1,0 };
int dy[4] = { 1,0,0,-1 };
vector<pair<int, int>> outline;
bool Check()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] == 1)
{
return true;
}
}
}
return false;
}
void Init()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
visit[i][j] = 0;
dist[i][j] = 0;
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
while (Check())
{
Init();
ans++;
queue<pair<int, int>> q;
q.push({ 0,0 });
visit[0][0] = 1;
// 치즈가 실내 공기와 맞닿아있는 부분을 저장(outline)
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= n || yy >= m)
{
continue;
}
dist[xx][yy]++;
if (!visit[xx][yy])
{
visit[xx][yy]++;
if (map[xx][yy] == 0)
{
q.push({ xx,yy });
}
else
{
outline.push_back({ xx,yy });
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// 2변 이상 공기와 접촉했다면 녹아없어진다.
if (dist[i][j] > 1 && map[i][j] == 1)
{
map[i][j] = 0;
}
}
}
}
cout << ans << endl;
return 0;
}