-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14503.cpp
More file actions
72 lines (66 loc) · 1.28 KB
/
14503.cpp
File metadata and controls
72 lines (66 loc) · 1.28 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
// 14503. 로봇 청소기
// 2019.05.22
// 시뮬레이션
#include<iostream>
using namespace std;
int map[51][51];
int ans = 1;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
int n, m;
void DFS(int x, int y, int d)
{
int xx, yy;
int dir = d;
// 총 4방향으로 이동할 수 있으므로 4번 돌림
for (int i = 0; i < 4; i++)
{
// 북,서,남,동(0,3,2,1) 순서로 변경
dir = (dir + 3) % 4;
xx = x + dx[dir];
yy = y + dy[dir];
// 이동한 칸이 벽
if (xx > n - 2 || xx < 1 || yy > m - 2 || yy < 1 || map[xx][yy] == 1)
{
continue;
}
// 청소 하지 않았다면
if (map[xx][yy] == 0)
{
ans++; //청소하고 카운트 증가
map[xx][yy] = 2; //청소했다고 표시
DFS(xx, yy, dir); //다음칸 방문을 위해 DFS함수 호출
return;
}
}
// 4방향의 칸 중 방문할 칸이 없으면 후진
dir = (d + 2) % 4; // 후진을 위한 방향 설정
xx = x + dx[dir];
yy = y + dy[dir];
// 벽일 경우 중단
if (map[xx][yy] == 1)
{
return;
}
else
{
DFS(xx, yy, d);
}
}
int main()
{
int d, x, y;
cin >> n >> m >> x >> y >> d;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
// 현재 위치를 청소한다.
map[x][y] = 2;
DFS(x, y, d);
cout << ans << endl;
return 0;
}