Repository navigation
Expand file tree
/
Copy pathMain_14503.java
More file actions
115 lines (93 loc) · 3.21 KB
/
Copy pathMain_14503.java
File metadata and controls
115 lines (93 loc) · 3.21 KB
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
package baekjoon;
import java.util.*;
import java.io.*;
public class Main_14503 {
static int n;
static int m;
static int r;
static int c;
static int direction; // 0: 북, 1: 동, 2: 남, 3: 서
static int[][] board;
static int[] dx = {-1, 0, 1, 0}; // 북서남동
static int[] dy = {0, -1, 0, 1};
static Queue<Pointer> queue;
static int answer;
static class Pointer {
int x;
int y;
int c;
public Pointer(int x, int y, int c) {
this.x = x;
this.y = y;
this.c = c;
}
}
public static void bfs(Pointer pointer) {
boolean flag = true;
queue.offer(pointer);
while (flag) {
Pointer current = queue.poll();
if (board[current.x][current.y] == 0) {
board[current.x][current.y] = 2;
answer++;
}
if (Main_14503.isMoveable(current)) {
flag = true;
} else {
flag = false;
}
}
}
public static boolean isMoveable(Pointer pointer) {
int nx = 0;
int ny = 0;
int ndir = 0;
// 네 방향 체크
for (int i = 1; i <= 4; i++) {
ndir = (pointer.c + i) % 4;
nx = pointer.x + dx[ndir];
ny = pointer.y + dy[ndir];
// 현재 방향 기준 왼쪽방향이 벽이거나 청소한 곳이라면 왼쪽방향으로 방향을 튼다
if (board[nx][ny] > 0) {
continue;
}
// 현재 방향 기준 왼쪽방향이 청소되지않았다면 이동
if (board[nx][ny] == 0) {
queue.offer(new Pointer(nx, ny, ndir));
return true;
}
}
// 네 방향 확인 했는데 다 청소된 경우 혹은 벽인 경우 후진한다
nx = pointer.x - dx[pointer.c];
ny = pointer.y - dy[pointer.c];
// 후진 하려는 방향이 벽인 경우 종료
if (board[nx][ny] == 1) {
return false;
} else if (Main_14503.isMoveable(new Pointer(nx, ny, pointer.c))) { // 후진한 방향이 벽이 아닌 경우
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
direction = Integer.parseInt(st.nextToken());
queue = new LinkedList<>();
board = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
if (direction == 1) direction = 3;
else if(direction == 3) direction = 1;
Main_14503.bfs(new Pointer(r, c, direction));
System.out.println(answer);
}
}