https://www.acmicpc.net/problem/3055
3055번: 탈출
사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제
www.acmicpc.net
[문제]
[풀이]
물이 찰 예정엔 고슴도치가 이동불가 => 물 먼저 처리할 것.
즉 , 탐색을 하기위해 우선순위를
1. 진행일수 2. 처리순서 로 하기위해서 우선순위큐를 이용한 BFS로 탐색하였다.
물탐색 : map이 '.' 이거나 'S' 인 경우에만 진행후 , '*'로 마킹.
고슴도치탐색 : map이 '.' 이거나 'D' 인 경우에만 진행후 'S'로 마킹.
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
|
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Main {
static int w,h;
static char[][] map;
static Point end;
static int[] dx={0,0,1,-1},dy={1,-1,0,0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] input = Arrays.stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
h = input[0]; w = input[1];
map = new char[h][w];
PriorityQueue<Point> q = new PriorityQueue<Point>();
for(int i=0;i<h;i++) {
map[i] = br.readLine().toCharArray();
for(int j=0;j<w;j++){
switch (map[i][j]){
case 'D' -> end = new Point(j,i,2,0);
case '*' -> q.add(new Point(j,i,0,0));
case 'S' -> q.add(new Point(j,i,1,0));
}
}
}
while (!q.isEmpty()){
Point cur = q.poll();
if(cur.y == end.y && cur.x==end.x){
System.out.println(cur.day);
return;
}
// 물 처리
if(cur.type==0){
for(int i=0;i<dx.length;i++){
int nextX = cur.x+dx[i];
int nextY = cur.y+dy[i];
if(!isRange(nextX,nextY)) continue;
if(map[nextY][nextX]=='.' || map[nextY][nextX]=='S'){
map[nextY][nextX] = '*';
q.add(new Point(nextX,nextY, cur.type, cur.day+1));
}
}
}
// 고슴도치 처리
if(cur.type==1){
for(int i=0;i<dx.length;i++){
int nextX = cur.x+dx[i];
int nextY = cur.y+dy[i];
if(!isRange(nextX,nextY)) continue;
if(map[nextY][nextX]=='.' || map[nextY][nextX]=='D'){
q.add(new Point(nextX,nextY, cur.type, cur.day+1));
map[nextY][nextX] = 'S';
}
}
}
}
System.out.println("KAKTUS");
}
private static boolean isRange(int x, int y) {
return x>=0&&y>=0&&x<w&&y<h;
}
private static class Point implements Comparable<Point>{
int x,y,day,type;
// type : [0:water] [1:viber] [2:cave]
public Point(int x, int y, int type, int day) {
this.x = x;
this.y = y;
this.day = day;
this.type = type;
}
@Override
public int compareTo(Point o) {
if(this.day==o.day) return this.type-o.type;
return this.day-o.day;
}
}
}
|
cs |
'알고리즘,PS > 백준' 카테고리의 다른 글
[BOJ] 백준 [17831] 대기업 승범이네 JAVA (0) | 2021.11.08 |
---|---|
[BOJ] 백준 [1135] 뉴스 전하기 JAVA (0) | 2021.11.08 |
[BOJ] 백준 [2307] 도로검문 JAVA (0) | 2021.11.05 |
[BOJ] 백준 [1781] 컵라면 JAVA (0) | 2021.11.02 |
[BOJ] 백준 [1461] 도서관 JAVA (0) | 2021.11.01 |