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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int[][] room;
private static int cleanedAreaCount;
private static boolean isActive;
private static int r;
private static int c;
private static int d;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st1 = new StringTokenizer(br.readLine());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st1.nextToken()); /* ํ ๊ฐ์ */
int m = Integer.parseInt(st1.nextToken()); /* ์ด ๊ฐ์ */
r = Integer.parseInt(st2.nextToken()); /* ์์น: ํ์ฌ X ์ขํ */
c = Integer.parseInt(st2.nextToken()); /* ์์น: ํ์ฌ Y ์ขํ */
d = Integer.parseInt(st2.nextToken()); /* ๋ฐฉํฅ: ๋ถ๋๋จ์(0, 1, 2, 3) */
cleanedAreaCount = 0;
isActive = true;
initRoom(n, m, br);
operateRobot();
System.out.println(cleanedAreaCount);
}
private static void initRoom(int n, int m, BufferedReader br) throws IOException {
room = new int[n][m];
for (int i = 0; i < n; i++) {
StringTokenizer st3 = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) room[i][j] = Integer.parseInt(st3.nextToken());
}
}
private static void operateRobot() {
while (isActive) {
// 1. ํ์ฌ ์นธ์ด ์์ง ์ฒญ์๋์ง ์์ ๊ฒฝ์ฐ, ํ์ฌ ์นธ์ ์ฒญ์ํ๋ค.
if (!isCleaned()) cleanArea();
if (isCleanedFourAdjacentDirections()) {
// 2. ํ์ฌ ์นธ์ ์ฃผ๋ณ 4์นธ ์ค ์ฒญ์๋์ง ์์ ๋น ์นธ์ด ์๋ ๊ฒฝ์ฐ,
if (canMoveBackward()) {
// 2-1. ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ ์ ์งํ ์ฑ๋ก ํ ์นธ ํ์งํ ์ ์๋ค๋ฉด ํ ์นธ ํ์งํ๊ณ 1๋ฒ์ผ๋ก ๋์๊ฐ๋ค.
moveBackward();
} else {
// 2-2. ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ ๋ค์ชฝ ์นธ์ด ๋ฒฝ์ด๋ผ ํ์งํ ์ ์๋ค๋ฉด ์๋์ ๋ฉ์ถ๋ค.
isActive = !isActive;
}
} else {
// 3. ํ์ฌ ์นธ์ ์ฃผ๋ณ 4์นธ ์ค ์ฒญ์๋์ง ์์ ๋น ์นธ์ด ์๋ ๊ฒฝ์ฐ,
changeDirection();
if (canMoveForward()) moveForward();
}
}
}
private static boolean isInRange(int r, int c) {
return r >= 0 && r < room.length && c >= 0 && c < room[0].length;
}
private static boolean isEmptyAndNotCleaned(int r, int c) {
return isInRange(r, c) && room[r][c] == 0;
}
private static boolean isWall(int r, int c) {
return !isInRange(r, c) || room[r][c] == 1;
}
private static boolean isCleaned() {
return room[r][c] == 2;
}
private static boolean isCleanedFourAdjacentDirections() {
return (!isEmptyAndNotCleaned(r - 1, c))
&& (!isEmptyAndNotCleaned(r + 1, c))
&& (!isEmptyAndNotCleaned(r, c - 1))
&& (!isEmptyAndNotCleaned(r, c + 1));
}
private static boolean canMoveForward() {
switch (d) {
case 0: return isEmptyAndNotCleaned(r - 1, c);
case 1: return isEmptyAndNotCleaned(r, c + 1);
case 2: return isEmptyAndNotCleaned(r + 1, c);
case 3: return isEmptyAndNotCleaned(r, c - 1);
default: throw new IllegalStateException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
private static boolean canMoveBackward() {
switch (d) {
case 0: return !isWall(r + 1, c);
case 1: return !isWall(r, c - 1);
case 2: return !isWall(r - 1, c);
case 3: return !isWall(r, c + 1);
default: throw new IllegalStateException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
private static void cleanArea() {
room[r][c] = 2;
cleanedAreaCount++;
}
private static void changeDirection() {
d = (d + 3) % 4;
}
private static void moveForward() {
int nextR = r, nextC = c;
switch (d) {
case 0: nextR--; break;
case 1: nextC++; break;
case 2: nextR++; break;
case 3: nextC--; break;
default: throw new IllegalStateException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
if (isInRange(nextR, nextC) && room[nextR][nextC] == 0) {
r = nextR;
c = nextC;
}
}
private static void moveBackward() {
int nextR = r, nextC = c;
switch (d) {
case 0: nextR++; break;
case 1: nextC--; break;
case 2: nextR--; break;
case 3: nextC++; break;
default: throw new IllegalStateException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
if (isInRange(nextR, nextC) && !isWall(nextR, nextC)) {
r = nextR;
c = nextC;
} else {
isActive = false;
}
}
}
|