-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
112 lines (97 loc) · 2.97 KB
/
Main.java
File metadata and controls
112 lines (97 loc) · 2.97 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static Integer R,C,minTime;
static Queue<Tile> floodList, beaverList;
static char[][] map;
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());
floodList = new LinkedList<>();
beaverList = new LinkedList<>();
map = new char[R][C];
for(int i=0; i<R; i++){
String str = br.readLine();
for(int j=0; j<C; j++){
map[i][j] = str.charAt(j);
if(map[i][j] == '*'){
floodList.add(new Tile(i,j));
} else if(map[i][j] == 'S') {
beaverList.add(new Tile(i,j));
}
}
}
flooding();
br.close();
}
public static void flooding() {
minTime = 0;
while(true){
minTime++;
if(beaverList.size() == 0){
System.out.println("KAKTUS");
return;
}
moveWater();
if(isGoal()){
System.out.println(minTime);
return;
}
}
}
public static boolean isGoal(){
int[] dx = {0,0,-1,1};
int[] dy = {-1,1,0,0};
int cnt = beaverList.size();
for(int i=0; i<cnt; i++){
Tile beaver = beaverList.poll();
for(int j=0; j<4; j++){
int nx = beaver.x +dx[j];
int ny = beaver.y +dy[j];
if(0<=nx && nx<R && 0<=ny && ny<C){
if(map[nx][ny] == 'D') {
return true;
}
if(map[nx][ny] == '.') {
map[nx][ny] = 'S';
beaverList.add(new Tile(nx,ny));
}
}
}
}
return false;
}
public static void moveWater() {
int[] dx = {0,0,-1,1};
int[] dy = {-1,1,0,0};
int cnt = floodList.size();
for(int i=0; i<cnt; i++){
Tile water = floodList.poll();
for(int j=0; j<4; j++){
int nx = water.x +dx[j];
int ny = water.y +dy[j];
if(0<=nx && nx<R && 0<=ny && ny<C){
if(map[nx][ny] == '.') {
map[nx][ny] = '*';
floodList.add(new Tile(nx,ny));
}
}
}
}
}
}
class Tile {
int x, y;
public Tile(int x, int y){
this.x = x;
this.y = y;
}
}