forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2636.cpp
More file actions
79 lines (70 loc) · 1.4 KB
/
Copy path2636.cpp
File metadata and controls
79 lines (70 loc) · 1.4 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
#include <iostream>
#include <vector>
using namespace std;
int N, M, ans, days = 0;
int board[100][100];
bool visited[100][100];
void dfs(int x, int y) {
visited[x][y] = true;
if (board[x][y] == 0) {
if (x > 0 && board[x - 1][y] == 0 && !visited[x - 1][y]) {
dfs(x - 1, y);
}
if (y > 0 && board[x][y - 1] == 0 && !visited[x][y - 1]) {
dfs(x, y - 1);
}
if (x < N - 1 && board[x + 1][y] == 0 && !visited[x + 1][y]) {
dfs(x + 1, y);
}
if (y < M - 1 && board[x][y + 1] == 0 && !visited[x][y + 1]) {
dfs(x, y + 1);
}
if (x > 0 && board[x - 1][y] == 1 && !visited[x - 1][y]) {
dfs(x - 1, y);
}
if (y > 0 && board[x][y - 1] == 1 && !visited[x][y - 1]) {
dfs(x, y - 1);
}
if (x < N - 1 && board[x + 1][y] == 1 && !visited[x + 1][y]) {
dfs(x + 1, y);
}
if (y < M - 1 && board[x][y + 1] == 1 && !visited[x][y + 1]) {
dfs(x, y + 1);
}
}
else if (board[x][y] == 1) {
board[x][y] = 2;
}
}
bool check() {
ans = 0;
days += 1;
int flag = true;
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
visited[i][j] = false;
if (board[i][j] == 2) {
ans += 1;
board[i][j] = 0;
}
else if (board[i][j] == 1) flag = false;
}
}
return flag;
}
int main()
{
int i, j;
cin >> N >> M;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
cin >> board[i][j];
}
}
do{
dfs(0, 0);
} while (!check());
cout << days << "\n" << ans;
return 0;
}