# 유형 : 탐색, 시뮬레이션

# 기존의 bfs를 사용하면서 동시에 물이 차오르는 것(물이 다음에 차오를 곳은 못간다) 을 생각해야 하기 때문에 큐를 한개 더 써서 물차오르는 것을 먼저 처리해주고, 그다음 고슴도치가 움직이게 하였다. 

# 간단히 말하면, 물 차오르는 큐를 하나 만들어 고슴도치가 움직이기전에 탐색을 돌려 차오르는 곳을 처리해주고, 그 다음 고슴도치가 움직일수있는 위치들을 반복문을 통해 조건문을 통과시키면서 처리한다.

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
98
99
100
101
102
103
package bj;
 
 
public class p3055 {
    static int R,C;
    static char arr[][];
    static int moveX[] = {0,1,0,-1};
    static int moveY[] = {-1,0,1,0};
    static int map[][];
    static Point start,Destination;
    static Queue<Point> river = new LinkedList<>();
    
    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());
        
        arr = new char[R][C];
        map = new int[R][C];
        for(int i=0; i<R; i++) {
            String str = br.readLine();
            for(int j=0; j<C; j++) {
                arr[i][j] = str.charAt(j);
                if(arr[i][j] == 'S')
                    start = new Point(j,i);
                else if(arr[i][j] =='*')
                    river.add(new Point(j,i));
                else if(arr[i][j] == 'D')
                    Destination = new Point(j,i);
            }
        }
        
        bfs(start);
//        for(int i=0; i<R; i++) {
//            for(int j=0; j<C; j++) {
//                System.out.print(map[i][j]+" ");
//            }System.out.println();
//        }
        if(map[Destination.y][Destination.x]==0) {
            System.out.println("KAKTUS");
        }else {
            System.out.println(map[Destination.y][Destination.x]-1);
        }
        
    }
    
    public static void bfs(Point p) {
        Queue<Point> queue = new LinkedList<>();
        map[p.y][p.x] = 1;
        queue.add(new Point(p.x, p.y));
        
        while(!queue.isEmpty()) {
            int currentRiver = river.size();
            
            for(int i=0; i<currentRiver; i++) {
                Point tmp = river.poll();
                int currentY = tmp.y;
                int currentX = tmp.x;
                
                for(int d=0; d<4; d++) {
                    int newY = currentY + moveY[d];
                    int newX = currentX + moveX[d];
                    
                    if(0<=newY && newY<&& 0<=newX && newX<C) {
                        if(arr[newY][newX]=='.') {
                            arr[newY][newX]='*';
                            river.add(new Point(newX,newY));
                        }
                    }
                }
            }
            
            int current = queue.size();
            for(int i=0; i<current; i++) {
                Point tmp = queue.poll();
                int y = tmp.y;
                int x = tmp.x;
                for(int d=0; d<4; d++) {
                    int newY = y + moveY[d];
                    int newX = x + moveX[d];
                    
                    if(0<=newY && newY<&& 0<=newX && newX<C) {
                        if(arr[newY][newX]!='*' && arr[newY][newX] !='X' && map[newY][newX]==0) {
                            map[newY][newX] = map[y][x] + 1;
                            queue.add(new Point(newX,newY));
                        }
                    }
                }
            }
            
        }
    }
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

'백준' 카테고리의 다른 글

#백준_1024 수열의 합 - Java 자바  (0) 2020.02.06
#백준_1068 트리 - Java 자바  (0) 2020.02.05
#백준_1043 거짓말 - Java 자바  (0) 2020.02.03
#백준_3190 뱀 - Java 자바  (0) 2020.02.02
#백준_1120 문자열 - Java 자바  (0) 2020.01.31

+ Recent posts