🔺 문제
4179번: 불!
입력의 첫째 줄에는 공백으로 구분된 두 정수 R과 C가 주어진다. 단, 1 ≤ R, C ≤ 1000 이다. R은 미로 행의 개수, C는 열의 개수이다. 다음 입력으로 R줄동안 각각의 미로 행이 주어진다. 각각의 문자
www.acmicpc.net
🔺 코드
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
|
import java.util.*;
import java.io.*;
public class Main {
static int[] dx = {0,0,-1,1};
static int[] dy = {1,-1,0,0};
static int R, C, res;
static char[][] map;
static Queue<State> fire, jh;
static class State {
int x, y, d;
public State(int x, int y, int d) {
this.x = x;
this.y = y;
this.d = d;
}
}
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];
jh = new LinkedList<>();
fire = new LinkedList<>();
// 입력받아 배열 초기화
for(int i = 0 ; i < R ; i++) {
map[i] = br.readLine().toCharArray();
for(int j = 0 ; j < C ; j++) {
// 불 위치
if(map[i][j] == 'F') {
fire.add(new State(i, j, 0));
}
// 지훈 위치
if(map[i][j] == 'J') {
jh.add(new State(i, j, 0));
}
}
}
System.out.println(bfs() ? res : "IMPOSSIBLE");
}
private static boolean bfs() {
while(!jh.isEmpty()) {
// 1. 불이 먼저 퍼짐
int size = fire.size();
for(int i = 0 ; i < size ; i++) {
State now = fire.poll();
for(int d = 0 ; d < 4 ; d++) {
int xx = now.x + dx[d];
int yy = now.y + dy[d];
if(xx < 0 || xx >= R || yy < 0 || yy >= C) continue;
if(map[xx][yy] == '#' || map[xx][yy] == 'F') continue;
map[xx][yy] = 'F';
fire.add(new State(xx, yy, now.d + 1));
}
}
// 2. 지훈이가 불을 피해 이동
size = jh.size();
for(int i = 0 ; i < size ; i++) {
State now = jh.poll();
for(int d = 0 ; d < 4 ; d++) {
int xx = now.x + dx[d];
int yy = now.y + dy[d];
// 💥 범위 벗어남 = 탈출 💥
if(xx < 0 || yy < 0 || xx >= R || yy >= C) {
res = now.d + 1;
return true;
}
if(map[xx][yy] == '#' || map[xx][yy] == 'F' || map[xx][yy] == 'J') continue;
map[xx][yy] = 'J';
jh.add(new State(xx, yy, now.d + 1));
}
}
}
return false;
}
}
|
cs |
✅ 해결 아이디어
✔ BFS
1. 지훈 & 불 위치 저장
2. 불 위치 퍼뜨림 (범위 벗어나거나 / 벽이거나 / 이미 방문한 경우, pass)
3. 불이 퍼지고 지훈 이동. → 가장자리면(=범위 벗어나면) 탈출 성공. (벽이거나 / 불이거나 / 이미 방문한 곳이면, pass)
💥 유의사항
• 불을 먼저 퍼뜨리고, 지훈이를 이동시켜야 함 (불에 탈 수 있는 위치에 지훈이가 있으면 안 되니까)
🔺 다른 풀이들
- 헐 내가 생각하던 풀이다!!
https://www.acmicpc.net/source/62966594
로그인
www.acmicpc.net
https://www.acmicpc.net/source/43888742
로그인
www.acmicpc.net
💬 느낀 점
다음에 풀라고 하면 풀 수 있을 것 같다!! (아마도.. 아마데우스..)
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
(참고)
[BOJ] 4179.불(BFS).java
#. Problemhttps://www.acmicpc.net/problem/4179* The copyright in this matter is in BOJ #. Resolution Process 1. Read and understand problem 2. Redefine the problem + abstract 3. Create solution plan (select Algorithm, Data structure) 4. Prove the plan (che
data-make.tistory.com
[백준] 4179: 불! (Java)
BOJ 4179: 불! https://www.acmicpc.net/problem/4179입력을 받으며 지훈의 위치와 불의 위치를 Queue에 넣는다.지훈의 위치가 이미 map의 가장자리에 있으면 탈출할 수 있기 때문에 바로 처리해준다.여기서 불
velog.io
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 7662번: 이중 우선순위 큐 (0) | 2023.07.11 |
---|---|
[백준/JAVA] 1644번: 소수의 연속합 (0) | 2023.07.10 |
[백준/JAVA] 19637번: IF문 좀 대신 써줘 (0) | 2023.07.08 |
[백준/JAVA] 7576번: 토마토 (0) | 2023.07.08 |
[백준/JAVA] 1012번: 유기농 배추 (0) | 2023.07.07 |