-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj14503
More file actions
75 lines (71 loc) · 1.5 KB
/
boj14503
File metadata and controls
75 lines (71 loc) · 1.5 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
#include <iostream>
#include <queue>
using namespace std;
struct State {
int x, y;
int d;
};
int N, M;
int Map[50][50];
bool cleaned[50][50];
queue<State>q;
const int dx[] = { -1,0,1,0 };
const int dy[] = { 0,1,0,-1 };
int Cleaner(int r,int c,int dir) {
int cnt = 1;
queue<State>q;
q.push({ r,c,dir });
cleaned[r][c] = true;
while (!q.empty()) {
int x = q.front().x;
int y = q.front().y;
int d = q.front().d;
q.pop();
bool flag = false;
//왼쪽부터 4방향 탐색
for (int i = 0; i < 4; i++) {
int nd = d - 1;
if (d == 0)
nd = 3;
int nx = x + dx[nd];
int ny = y + dy[nd];
//왼쪽 방향에 청소할 공간이 없다면 그 방향으로 회전하고 다시 탐색
if (Map[nx][ny] == 1 || cleaned[nx][ny] == true) {
d = nd;
continue;
}
//아직 청소하지 않은 공간 존재하면
if (cleaned[nx][ny] == false) {
cleaned[nx][ny] = true;
//그 방향으로 회전한 다음 한 칸 전진
q.push({ nx,ny,nd });
//청소한 칸 개수 세기
cnt++;
flag = true;
break;
}
}
//네 방향 모두 청소가 이미 되어있거나 벽인 경우
if (flag == false) {
//후진
int bx = x - dx[d];
int by = y - dy[d];
//뒤쪽 방향이 벽이 아니면
if (Map[bx][by] != 1)
q.push({ bx,by,d });
}
}
return cnt;
}
int main() {
cin >> N >> M;
int r, c, d;
cin >> r >> c >> d;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> Map[i][j];
}
}
cout << Cleaner(r, c, d) << '\n';
return 0;
}