-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3055_ํ์ถ.java
More file actions
60 lines (55 loc) ยท 2.64 KB
/
3055_ํ์ถ.java
File metadata and controls
60 lines (55 loc) ยท 2.64 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int R, C, ans;
static char[][] map;
static int[] dx = new int[]{1,-1,0,0}; //๋ฐฉํฅ๋ฒกํฐ
static int[] dy = new int[]{0,0,1,-1};
static int[] start; //์์์์น ๋ฐฐ์ด
static Queue<int[]> queue = new ArrayDeque<>();
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];
for(int i=0; i<R; i++) { //๋งต ์
๋ ฅ
String input = br.readLine();
for(int j=0; j<C; j++) {
char idx = input.charAt(j);
if(idx == 'S') { //๊ณ ์ด๋์น ์์์์น์ผ ๊ฒฝ์ฐ
start = new int[]{i,j};
} else if(idx == '*') { //๋ฌผ์ด ์ฐจ์๋ ๊ฒฝ์ฐ
queue.offer(new int[]{i,j,0, 1}); //ํ์ฐ์ ์ํ ํ ์ถ๊ฐ
}
map[i][j] = idx;
}
}
queue.offer(new int[]{start[0], start[1], 0, 0}); //์ขํ,์๊ฐ,ํ๋๊ทธ(๋น๋ฒ์ธ์ง ๋ฌผ์ธ์ง)
bfs();
if(ans == 0) System.out.println("KAKTUS"); //๊ฒฝ๋ก๊ฐ ์์ ๊ฒฝ์ฐ
else System.out.println(ans);
}
private static void bfs() {
while(!queue.isEmpty()) {
int[] idx = queue.poll();
for(int i=0; i<4; i++) {
int nx = idx[0] + dx[i];
int ny = idx[1] + dy[i];
if(nx<0 || ny<0 || nx>=R || ny>=C || map[nx][ny] == 'X') continue; //๋งต๋ฐ์ผ๋ก ๋ฒ์ด๋ ๊ฒฝ์ฐ ๋ฐ ๋ค์ ์ด๋์์ ์ธ ์ขํ๊ฐ ๋ ์ผ ๊ฒฝ์ฐ
if(idx[3] == 1) { //ํ์์ ๊บผ๋ธ ๊ฐ์ด ๋ฌผ์ผ ๊ฒฝ์ฐ
if(map[nx][ny] == 'D' || map[nx][ny] == '*') continue; //๋ฌผ์ ํ์ฐ์ ๋น๋ฒ์๊ตด, ์ด๋ฏธ ์ฐจ์๋ ๊ฒฝ์ฐ์๋ ์๋จ
map[nx][ny] = '*'; //๋งต ๊ฐ ๊ฐฑ์
queue.offer(new int[]{nx,ny,0,1}); //ํ๋๊ทธ 1๋ก ํ์ ์ถ๊ฐ
} else { //๋น๋ฒ์ผ ๊ฒฝ์ฐ ์ด๋๋ก์ง
if(map[nx][ny] == 'D') ans = idx[2]+1; //์๊ตด๋ก ๋๋ฌํ ๊ฒฝ์ฐ
if(map[nx][ny] == '*' || map[nx][ny] == 'S') continue; //๋ฌผ์ด๋ ๋ฐฉ๋ฌธํ๋ ๊ณณ์ ๋ง๋๋ฉด ํ์ถ
map[nx][ny] = 'S'; //๋ฐฉ๋ฌธ์ฒดํฌ์ฉ ๊ฐ ๋ณ๊ฒฝ
queue.offer(new int[]{nx,ny,idx[2]+1,0}); //ํ๋๊ทธ 0์ผ๋ก ํ์ ์ถ๊ฐ
}
}
}
}
}