-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_3055.java
More file actions
132 lines (103 loc) · 3.41 KB
/
baekjoon_3055.java
File metadata and controls
132 lines (103 loc) · 3.41 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.*;
import java.util.*;
public class baekjoon_3055 {
static int[][] water;
static char[][] map;
static int R, C, sr, sc, ddr, ddc, result;
static char DOT = '.', D = 'D', S = 'S';
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new char[R][C];
water = new int[R][C];
for(int i=0;i<R;i++){
Arrays.fill(water[i], Integer.MAX_VALUE);
}
for(int i=0;i<R;i++) {
map[i] = br.readLine().toCharArray();
for(int j=0;j<C;j++) {
if(map[i][j] == 'D') {
ddr = i;
ddc = j;
}
if(map[i][j] == 'S') {
sr = i;
sc = j;
}
}
}
flood();
// print();
boolean flag = proceed();
System.out.println((flag)? result : "KAKTUS" );
br.close();
}
public static boolean proceed() {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{sr, sc, 0});
boolean[][] visited = new boolean[R][C];
while(!queue.isEmpty()){
int[] cur = queue.poll();
int r = cur[0];
int c = cur[1];
int w = cur[2];
int nw = w + 1;
for(int i=0;i<4;i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if(nr < 0 || nc < 0 || nr >= R || nc >= C || map[nr][nc] == 'X' || visited[nr][nc]) {
continue;
}
int seq = water[nr][nc];
if(seq <= nw) { continue; }
queue.add(new int[] {nr, nc, nw});
visited[nr][nc] = true;
if(nr == ddr && nc == ddc) {
result = nw;
return true;
}
}
}
return false;
}
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
public static void flood() {
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[R][C];
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
if(map[i][j] == '*') {
queue.add(new int[]{i, j, 0});
visited[i][j] = true;
}
}
}
while(!queue.isEmpty()) {
int[] cur = queue.poll();
int r = cur[0];
int c = cur[1];
int w = cur[2];
water[r][c] = w;
for(int i=0;i<4;i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if(nr < 0 || nc < 0 || nr >= R || nc >= C || map[nr][nc] != DOT || visited[nr][nc]) {
continue;
}
queue.add(new int[]{nr, nc, w + 1});
visited[nr][nc] = true;
}
}
}
public static void print() {
for(char[] arr : map){
for(char c : arr) {
System.out.printf("%c", c);
}
System.out.println();
}
System.out.println();
}
}