3055번: 탈출
사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제
www.acmicpc.net
package 백준.자바.탈출_3055;
import java.io.*;
import java.util.*;
public class Main {
private int N, M;
private int[] dx = {0, 0, -1, 1};
private int[] dy = {1, -1, 0, 0};
private char[][] arr;
private boolean[][] visited;
private Queue<Pair> q = new LinkedList<>();
private List<Pair> floodZone = new LinkedList<>();
void flood(){
int s = floodZone.size();
for(int i = 0; i < s; i++){
for(int j = 0; j < 4; j++){
int nx = floodZone.get(i).x + dx[j];
int ny = floodZone.get(i).y + dy[j];
if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
if(arr[nx][ny] != 'X' && arr[nx][ny] != 'D' && arr[nx][ny] != '*'){
arr[nx][ny] = '*';
floodZone.add(new Pair(nx, ny, 0));
}
}
}
}
int bfs(int x, int y){
q.offer(new Pair(x, y, 0));
visited[x][y] = true;
int turn = -1;
while(!q.isEmpty()){
Pair p = q.poll();
if(turn < p.count) {
flood();
turn = p.count;
}
for(int i = 0; i < 4; i++){
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
if(arr[nx][ny] == 'D') return p.count+1;
if(!visited[nx][ny] && arr[nx][ny] == '.') {
q.offer(new Pair(nx, ny, p.count + 1));
visited[nx][ny] = true;
}
}
}
return -1;
}
class Pair{
int x;
int y;
int count;
public Pair(int x, int y, int count) {
this.x = x;
this.y = y;
this.count = count;
}
}
public void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
int x = 0, y= 0;
arr = new char[N][M];
visited = new boolean[N][M];
for(int i = 0; i < N; i++){
String s = br.readLine();
for(int j = 0; j < M; j++) {
if (s.charAt(j) == 'S') {
arr[i][j] = '.';
x = i;
y = j;
} else if (s.charAt(j) == '*'){
arr[i][j] = '*';
floodZone.add(new Pair(i, j, 0));
}
else arr[i][j] = s.charAt(j);
}
}
int result = bfs(x, y);
System.out.println(result != -1 ? result : "KAKTUS");
}
public static void main(String[] args) throws Exception{
new Main().solution();
}
}
'문제풀이 > 백준' 카테고리의 다른 글
A와B 12904 Java (0) | 2024.01.25 |
---|---|
기타레슨 2343 Java (0) | 2024.01.25 |
숨바꼭질2 12851 Java (0) | 2024.01.21 |
치킨배달 15686 Java (1) | 2024.01.21 |
치즈 2638 Java (1) | 2024.01.21 |