-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14503_로봇 청소기.cpp
87 lines (80 loc) · 1.31 KB
/
14503_로봇 청소기.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
#include <iostream>
using namespace std;
int board[50][50];
int visited[50][50];
int dir;
int dirX[4] = { 0, -1, 0, 1 };
int dirY[4] = { -1, 0, 1, 0 };
int cnt = 1;
int main(void)
{
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);
int n, m, r, c, d;
cin >> n >> m >> r >> c >> d;
dir = d;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int tempNum;
cin >> tempNum;
board[i][j] = tempNum;
visited[i][j] = tempNum;
}
}
int curX = r;
int curY = c;
visited[curX][curY] = 1;
int dirIdx = 0;
while (1)
{
dirIdx = 0;
for (; dirIdx < 4; dirIdx++)
{
int newX = curX + dirX[(dir - dirIdx + 4) % 4];
int newY = curY + dirY[(dir - dirIdx + 4) % 4];
if (board[newX][newY] == 0 && visited[newX][newY] == 0)
{
curX = newX;
curY = newY;
dir = ((dir - dirIdx + 4) % 4 + 3) % 4;
visited[curX][curY] = 1;
cnt++;
break;
}
}
if (dirIdx == 4)
{
int newX, newY;
switch (dir)
{
case 0:
newX = curX + 1;
newY = curY;
break;
case 1:
newX = curX;
newY = curY - 1;
break;
case 2:
newX = curX - 1;
newY = curY;
break;
case 3:
newX = curX;
newY = curY + 1;
break;
}
if (board[newX][newY] == 0)
{
curX = newX;
curY = newY;
}
else
break;
}
}
cout << cnt;
}