๐ ๋ฌธ์
๐ ์์ด๋์ด
bfs๋ก ํด๊ฒฐํ์์ต๋๋ค.
ํ ํ์์๋ ๋ถ์ด ํผ์ง๋ ๊ฒฝ์ฐ์ ๋ด๊ฐ ์ด๋ํ๋ ๊ฒฝ์ฐ๋ฅผ ๋ชจ๋ ์๊ฐํด์ผํ์ต๋๋ค. ๋ํ ์ด๋ฒ ํ์์ ๋ถ์ด ์ฎ๊ฒจ์ง๋๊ณณ์๋ ์์ ์ด ์ด๋ํ๋ฉด ์๋๋ค๋ ์กฐ๊ฑด์ด์์ต๋๋ค.
์ ์ ๊ฒฝ์ฐ ๋ถ์ด ์ฎ๊ฒจ ๋ถ์ด์ง๋ ๊ฒฝ์ฐ๋ฅผ ๋ชจ๋ ๊ณ์ฐํ ํ์ ์ด๋ํ์ง ์๊ณ ์ด๋ํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ด๋ํ ํ์, ๋ถ์ด ์ด๋ํ๋๋ก ํ์์ต๋๋ค. ์ด๋ ๊ฒ ํ ํ ๋ค์ํด์ ์์ ์ ์์น๊ฐ ๋ถ๋ก ๋ณํ ๊ฒฝ์ฐ ํด๋น ์นธ์ ์ด์ ์ ๊ฐ ์ ์๋ ์นธ์ด์๋๋ฐ ์ด๋ํ๊ฒ์ผ๋ก ํ์ฌ ๋ฌด์ํ๋ ๋ฐฉ์์ผ๋ก ์งํํ์์ต๋๋ค.
์๊ทผ์ด๊ฐ ์ด๋ฏธ ๋ค๋ ๊ฐ ์์น๋ @๋ก ํ๊ธฐํ์ฌ ์๊ทผ์ด๊ฐ ๋ค์ ๋ฐฉ๋ฌธํ์ง ์๋๋ก ํ์๊ณ , ๋ถ์ด ์ฎ๊ฒจ๋ถ๋ ์นธ์ '.' ๊ณผ '@'๋ก ํ์ฌ ๋ถ์ด ์ฎ๊ฒจ ๋ถ์ ์ ์๋๋ก ํ์์ต๋๋ค. ์ดํ์ ๋ง์ฝ ์๊ทผ์ด๊ฐ ์ธ๊ฐ์ ๋์ฐฉํ ๊ฒฝ์ฐ ์ด๋ํ ์นธ์ ๊ฐ์๋ฅผ ๋ฆฌํดํ๋๋ก ํ๊ณ ๋ง์ฝ ์๊ทผ์ด๊ฐ ๋ ์ด์๊ฐ ์ ์๋ ๊ณณ์ด ์๋ค๋ฉด -1์ ๋ฆฌํดํ์ฌ IMPOSSIBLE์ ํ์ ํ๋๋ก ํ์์ต๋๋ค.
๐ ํ์ด
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Boj_5427_๋ถ {
private static int w, h;
private static char[][] map;
private static final int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static Queue<Point> me = new LinkedList<>();
private static Queue<Point> fires = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(br.readLine());
while (tc-- > 0) {
st = new StringTokenizer(br.readLine());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
map = new char[h][w];
me.clear();
fires.clear();
for (int i = 0; i < h; i++) {
map[i] = br.readLine().toCharArray();
for (int j = 0; j < w; j++) {
if (map[i][j] == '*') {
fires.add(new Point(i, j));
} else if (map[i][j] == '@') {
me.add(new Point(i, j));
}
}
}
int mvCnt = findExit();
if (mvCnt == -1) {
sb.append("IMPOSSIBLE");
} else sb.append(mvCnt);
sb.append("\n");
}
System.out.println(sb);
}
private static int findExit() {
int mvCnt = 0;
while (!me.isEmpty()) {
//๋ด๊ฐ ๊ฐ์์๋๊ฑฐ ํ์นธ ๊ฐ๊ธฐ
mvCnt++;
int myCnt = me.size();
for (int i = 0; i < myCnt; i++) {
Point now = me.poll();
if (map[now.x][now.y] == '*') continue;//๋ด๊ฐ ์๋๊ณณ์ ๋ถ์ด์์ผ๋ฉด ๊ทธ๊ณณ์ผ๋ก๋ ๊ณผ๊ฑฐ์ ๊ฐ๋ฉด ์๋๊ธฐ ๋๋ฌธ
if (now.x == 0 || now.y == 0 || now.x == h - 1 || now.y == w - 1) {
return mvCnt;
}
for (int[] dir : dirs) {
int mvX = now.x + dir[0];
int mvY = now.y + dir[1];
if (0 <= mvX && 0 <= mvY && mvX < h && mvY < w && map[mvX][mvY] == '.') {
map[mvX][mvY] = '@'; //์๊ทผ์ด๊ฐ ์ด๋ฏธ ๋ค๋
๊ฐ๋คํ์
me.add(new Point(mvX, mvY));
}
}
}
//๋ถ ์ฎ๊ธฐ๊ธฐ
int fireCnt = fires.size();
for (int i = 0; i < fireCnt; i++) {
Point poll = fires.poll();
for (int[] dir : dirs) {
int mvX = poll.x + dir[0];
int mvY = poll.y + dir[1];
if (0 <= mvX && 0 <= mvY && mvX < h && mvY < w && (map[mvX][mvY] == '.' || map[mvX][mvY] == '@')) {
map[mvX][mvY] = '*';
fires.add(new Point(mvX, mvY));
}
}
}
}
return -1;
}
}
'๋ฐฑ์ค(boj)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA]11437 LCA ๊ณตํต ๋ถ๋ชจ ๋ ธ๋ ์ฐพ๊ธฐ(๊ณจ๋ 3) (0) | 2023.09.20 |
---|---|
[JAVA]13418 ํ๊ต ํ๋ฐฉํ๊ธฐ, ๊ณจ๋ 3 (0) | 2023.08.21 |
[JAVA]2531 ํ์ ์ด๋ฐฅ (0) | 2023.08.10 |
[JAVA]2230 ์ ๊ณ ๋ฅด๊ธฐ, ํฌ ํฌ์ธํฐ(๊ณจ๋5) (0) | 2023.08.09 |
[JAVA]11780 ํ๋ก์ด๋, ํ๋ก์ด๋ ์์ (1) | 2023.08.09 |