-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2573.cpp
More file actions
141 lines (132 loc) · 2.9 KB
/
2573.cpp
File metadata and controls
141 lines (132 loc) · 2.9 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
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
138
139
140
141
// 2573. 빙산
// 2019.05.20
// BFS
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int map[301][301];
int visit[301][301];
// 빙산 구조체
struct iceberg
{
int x; // x위치
int y; // y위치
int height; // 높이
};
queue<iceberg> icebergs;
queue<iceberg> nextIcebergs;
bool flag;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
if (map[i][j] != 0)
{
icebergs.push({ i,j,map[i][j] });
}
}
}
int ans = 0;
while (1)
{
ans++;
for (int i = 0; i < 301; i++)
{
fill(visit[i], visit[i] + 301, 0);
}
while (!icebergs.empty())
{
int height = icebergs.front().height;
int ix = icebergs.front().x;
int iy = icebergs.front().y;
icebergs.pop();
for (int j = 0; j < 4; j++)
{
int x = ix + dx[j];
int y = iy + dy[j];
if (x < 0 || y < 0 || x >= n || y >= m)
{
continue;
}
if (map[x][y] == 0)
{
height--;
}
}
// 녹은값을 저장한 빙산들의 정보를 next에 넣어줌.
nextIcebergs.push({ ix,iy,height });
}
// nextIcebergs에있는걸 icebergs로 넣어줌
while (!nextIcebergs.empty())
{
// 값이 0보다 클때(빙산이 존재할때)만 넣어준다.
if (nextIcebergs.front().height > 0)
{
icebergs.push(nextIcebergs.front());
map[nextIcebergs.front().x][nextIcebergs.front().y] = nextIcebergs.front().height;
nextIcebergs.pop();
}
// 값이 0이하면 녹아 없어진것이므로 맵의 값만 0으로 바꾸고 icebergs에 넣어주진 않는다.
else
{
map[nextIcebergs.front().x][nextIcebergs.front().y] = 0;
nextIcebergs.pop();
}
}
// 다 녹을때까지 분리되지 않았다면 0출력
if (icebergs.empty())
{
cout << 0 << endl;
return 0;
}
// 2개로 나누어졌는지 체크
else
{
// icebergs에 맨앞에 들어있는 정보를 기준으로 BFS로 visit 표시
queue<pair<int, int>> q;
q.push({ icebergs.front().x , icebergs.front().y });
visit[icebergs.front().x][icebergs.front().y] = 1;
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 || map[xx][yy]==0)
{
continue;
}
if (!visit[xx][yy])
{
visit[xx][yy] = 1;
q.push({ xx,yy });
}
}
}
// icebergs를 순회하면서 만일 visit값이 0이 아니면 위의 BFS에서 방문하지 않았으므로 2개이상으로 나누어진것.
for (int i = 0; i < icebergs.size(); i++)
{
// 나누어졌다면 ans 출력
if (visit[icebergs.front().x][icebergs.front().y]==0)
{
cout << ans << endl;
return 0;
}
icebergs.push(icebergs.front());
icebergs.pop();
}
}
}
return 0;
}