-
Notifications
You must be signed in to change notification settings - Fork 0
/
21922_학부연구생민상.cpp
97 lines (86 loc) · 2.06 KB
/
21922_학부연구생민상.cpp
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
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long
#define INF 987654321
#define MAX 2001
#define PRIMEMAX 1000001
#define int ll
using namespace std;
typedef pair<int, int> p;
// 상, 우, 하, 좌
p Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int N, M;
int board[MAX][MAX];
bool visited[MAX][MAX][4];
bool valid(int x, int y){
return 0 <= x && x < N && 0 <= y && y < M;
}
int turnDir(int bb, int d){
if(bb == 1){
if(d == 1 || d == 3) return -1;
else return d;
}else if(bb == 2){
if(d == 0 || d == 2) return -1;
else return d;
}else if(bb == 3){
if(d == 0)
return 1;
else if(d == 1)
return 0;
else if(d == 2)
return 3;
else if(d == 3)
return 2;
}else if(bb == 4){
if(d == 0)
return 3;
else if(d == 1)
return 2;
else if(d == 2)
return 1;
else if(d == 3)
return 0;
}
return d;
}
void dfs(int x, int y, int d){
visited[x][y][d] = true;
d = turnDir(board[x][y], d);
if(d == -1) return;
int nx = x + Dir[d].first;
int ny = y + Dir[d].second;
if(valid(nx, ny) && !visited[nx][ny][d])
dfs(nx, ny, d);
return;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
memset(visited, false, sizeof(visited));
vector<p> air;
cin >> N >> M;
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j){
cin >> board[i][j];
if(board[i][j] == 9){
air.push_back({i, j});
board[i][j] = 0;
}
}
for(p coord: air)
for(int i = 0; i < 4; ++i)
dfs(coord.first, coord.second, i);
int ans = 0;
for(int i = 0; i <N; ++i){
for(int j = 0; j < M; ++j){
bool flag = false;
for(int k = 0; k < 4; ++k)
if(visited[i][j][k])
flag = true;
if(flag)
ans++;
}
}
cout << ans << endl;
return 0;
}