-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14503.java
84 lines (64 loc) · 1.5 KB
/
14503.java
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
import java.util.*;
public class Main {
static class Robot {
int x, y, direction;
Robot(int x, int y, int direction) {this.x=x; this.y=y; this.direction=direction;}
}
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Robot r = new Robot(sc.nextInt(), sc.nextInt(), sc.nextInt());
if(r.direction == 1)
r.direction = 3;
else if(r.direction == 3)
r.direction = 1;
int[][] map = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
map[i][j] = sc.nextInt();
}
}
// 북, 서, 남, 동
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, -1, 0, 1};
int cnt = 0;
boolean end = false;
while(!end) {
// 청소 실시
if(map[r.x][r.y] == 0) {
map[r.x][r.y] = 2;
cnt++;
}
// 순서대로 검사해서 갈 수 있는 곳 발견되면 이동
boolean foundToGo = false;
for(int i=1; i<5; i++) {
int direction = (r.direction + i) % 4;
int x = r.x+dx[direction];
int y = r.y+dy[direction];
// 빈칸이면 이동
if(map[x][y] == 0) {
r.x=x;
r.y=y;
r.direction = direction;
foundToGo = true;
break;
}
}
// 4방향 모두 갈 수 없었다면
if(!foundToGo) {
int rear = (r.direction+2) % 4;
int x = r.x+dx[rear];
int y = r.y+dy[rear];
if(map[x][y] == 1) {
end = true;
}
else {
r.x = x;
r.y = y;
}
}
}
System.out.println(cnt);
}
}