Study Algorithm
백준_14503_로봇청소기
_리므_
2020. 5. 2. 19:12
삼성 SW 역량테스트 기출문제 로봇청소기입니다.
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_14503_로봇청소기 {
static int R, C, clean, startY, startX, startD;
static int[][] map, tmpCnt;
static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
if (st.hasMoreTokens()) {
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
}
map = new int[R][C];
tmpCnt = new int[R][C];
visited = new boolean[R][C];
st = new StringTokenizer(br.readLine());
if (st.hasMoreTokens()) {
startY = Integer.parseInt(st.nextToken());
startX = Integer.parseInt(st.nextToken());
startD = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < R; i++) {
st = new StringTokenizer(br.readLine());
if (st.hasMoreTokens()) {
for (int j = 0; j < C; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
}
clean = 1;
bfs(startY, startX, startD);
System.out.println(clean);
}
private static void bfs(int startY, int startX, int startD) {
Queue<int[]> que = new LinkedList<>();
que.offer(new int[] { startY, startX, startD });
int cnt = 0;
tmpCnt[startY][startX] = clean;
visited[startY][startX] = true;
while (!que.isEmpty()) {
int[] current = que.poll();
int y = current[0];
int x = current[1];
int d = current[2];
if (cnt < 4) {
if (d == 0) {
if (safe(y, x - 1) && map[y][x - 1] == 0) {
if (!visited[y][x - 1]) {
visited[y][x - 1] = true;
x -= 1;
cnt = 0;
clean++;
tmpCnt[y][x] = clean;
} else {
cnt++;
}
} else {
cnt++;
}
d = 3;
} else if (d == 1) {
if (safe(y - 1, x) && map[y - 1][x] == 0) {
if (!visited[y - 1][x]) {
visited[y - 1][x] = true;
y -= 1;
cnt = 0;
clean++;
tmpCnt[y][x] = clean;
} else {
cnt++;
}
} else {
cnt++;
}
d = 0;
} else if (d == 2) {
if (safe(y, x + 1) && map[y][x + 1] == 0) {
if (!visited[y][x + 1]) {
visited[y][x + 1] = true;
x += 1;
cnt = 0;
clean++;
tmpCnt[y][x] = clean;
} else {
cnt++;
}
} else {
cnt++;
}
d = 1;
} else if (d == 3) {
if (safe(y + 1, x) && map[y + 1][x] == 0) {
if (!visited[y + 1][x]) {
visited[y + 1][x] = true;
y += 1;
cnt = 0;
clean++;
tmpCnt[y][x] = clean;
} else {
cnt++;
}
} else {
cnt++;
}
d = 2;
}
} else {
cnt = 0;
if (!back(y, x, d)) {
return;
} else {
if (d == 0) {
y += 1;
} else if (d == 1) {
x -= 1;
} else if (d == 2) {
y -= 1;
} else {
x += 1;
}
if (map[y][x] != 0) {
return;
}
}
}
que.offer(new int[] { y, x, d });
}
}
public static boolean back(int y, int x, int d) {
if (d == 0) {
if (safe(y + 1, x)) {
return true;
} else {
return false;
}
} else if (d == 1) {
if (safe(y, x - 1)) {
return true;
} else {
return false;
}
} else if (d == 2) {
if (safe(y - 1, x)) {
return true;
} else {
return false;
}
} else {
if (safe(y, x + 1)) {
return true;
} else {
return false;
}
}
}
public static boolean safe(int y, int x) {
if (y >= 0 && y < R && x >= 0 && x < C) {
return true;
} else {
return false;
}
}
}
|
cs |
사실 내가 푼 방법은 '빡'구현이라 문제를 푸는 속도면에서 좋지 않다.
해당 코드는 정말 하나하나 확인하고 싶을 때, 볼만한 코드이다.
다음에 풀 때엔, 좀 더 코드를 간략화할 필요가 있을 것 같다.
필요 스킬 : BFS