Skip to content

Commit 34604f4

Browse files
committed
[Simulation] baekjoon-14503
1 parent bf85095 commit 34604f4

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| 08 | | [Baekjoon-16927 배열 돌리기 2](./src/Simulation/P16927) | |
3838
| 09 | | [Baekjoon-16935 배열 돌리기 3](./src/Simulation/P16935) | |
3939
| 10 | | [Baekjoon-14499 주사위 굴리기](./src/Simulation/P14499) | 삼성 SW 역량 테스트 기출 |
40+
| 11 | | [Baekjoon-14503 로봇 청소기](./src/Simulation/P14503) | 삼성 SW 역량 테스트 기출 |
4041

4142
## String
4243

src/Simulation/P14503/Main.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package Simulation.P14503;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int N, M, i, j, d, sum;
9+
static int[][] map;
10+
11+
static int[] di = {-1, 0, 1, 0};
12+
static int[] dj = {0, 1, 0, -1};
13+
14+
public static void main(String[] args) throws Exception {
15+
System.setIn(new FileInputStream("src/Simulation/P14503/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
19+
N = stoi(st.nextToken());
20+
M = stoi(st.nextToken());
21+
22+
st = new StringTokenizer(br.readLine());
23+
i = stoi(st.nextToken());
24+
j = stoi(st.nextToken());
25+
d = stoi(st.nextToken());
26+
27+
map = new int[N][M];
28+
for (int r = 0; r < N; r++) {
29+
st = new StringTokenizer(br.readLine());
30+
for (int c = 0; c < M; c++) map[r][c] = stoi(st.nextToken());
31+
}
32+
33+
int dcnt = 0;
34+
map[i][j] = 2; sum ++;
35+
while (true) {
36+
int to_d = d-1 < 0 ? 3 : d-1;
37+
int to_i = i + di[to_d], to_j = j + dj[to_d];
38+
39+
if (isValidPath(to_i, to_j)) {
40+
if (map[to_i][to_j] == 0) {
41+
d = to_d; i = to_i; j = to_j; dcnt = 0;
42+
map[i][j] = 2; sum ++;
43+
} else {
44+
d = to_d; dcnt ++;
45+
}
46+
}
47+
48+
if (dcnt == 4) {
49+
to_i = i - di[to_d];
50+
to_j = j - dj[to_d];
51+
52+
if (!isValidPath(to_i, to_j) || map[to_i][to_j] == 1) break;
53+
54+
i = to_i;
55+
j = to_j;
56+
dcnt = 0;
57+
}
58+
}
59+
60+
System.out.println(sum);
61+
}
62+
63+
static boolean isValidPath(int i, int j) {
64+
return 0 <= i && i < N && 0 <= j && j < M;
65+
}
66+
67+
static int stoi(String s) { return Integer.parseInt(s); }
68+
}

src/Simulation/P14503/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-14503] 로봇 청소기
2+
3+
![image](https://user-images.githubusercontent.com/22045163/107954499-dcef6a00-6fdf-11eb-8845-4efc6ae799e0.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/107954534-e8db2c00-6fdf-11eb-9e13-77bef2db0510.png)

src/Simulation/P14503/input.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
11 10
2+
7 4 0
3+
1 1 1 1 1 1 1 1 1 1
4+
1 0 0 0 0 0 0 0 0 1
5+
1 0 0 0 1 1 1 1 0 1
6+
1 0 0 1 1 0 0 0 0 1
7+
1 0 1 1 0 0 0 0 0 1
8+
1 0 0 0 0 0 0 0 0 1
9+
1 0 0 0 0 0 0 1 0 1
10+
1 0 0 0 0 0 1 1 0 1
11+
1 0 0 0 0 0 1 1 0 1
12+
1 0 0 0 0 0 0 0 0 1
13+
1 1 1 1 1 1 1 1 1 1

0 commit comments

Comments
 (0)