본문 바로가기
알고리즘/그래프

[java 백준] 실버 1/16918번 봄버맨

by Meaning_ 2022. 5. 14.
728x90
반응형

https://www.acmicpc.net/problem/16918

 

16918번: 봄버맨

첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 
public class Main {
    public static class Node{
        int x;
        int y;
        public Node(int x,int y){
            this.x=x;
            this.y=y;
        }
    }
 
    public static int r,c,n;
    public static char [][]arr;
 
    public static int dirX[]={-1,1,0,0};
    public static int dirY[]={0,0,-1,1};
 
    public static int[][]bombCount;
    public static boolean[][]check;
 
    public static void Boom(){
        for(int i=1;i<=n;i++){
            if(i==1){
                continue;
            }
            if(i%2==0){
                setBomb(i);
            }
            else{
                BFS(i);
            }
        }
 
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
 
    public static void setBomb(int sec){
        //짝수 초 일떄는 폭탄 채워주고
        //bombCount는 현재 sec에서 3더해주기!
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(arr[i][j]=='.'){
                    arr[i][j]='O';
                    bombCount[i][j]=sec+3;
                }
            }
        }
 
    }
 
    public static void BFS(int sec){
        Queue<Node>queue=new LinkedList<Node>();
        //폭파해야 하는 애들을 BFS로 처리
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(arr[i][j]=='O'){
                    //3초 지난애들은 폭파시켜주기 위해 queue에 넣음
                    if(sec==bombCount[i][j]){
                        queue.add(new Node(i,j));
                    }
                }
            }
        }
 
        while(!queue.isEmpty()){
            Node node=queue.poll();
            arr[node.x][node.y]='.';
            bombCount[node.x][node.y]=sec+3;
            for(int i=0;i<4;i++){
                int nx=node.x+dirX[i];
                int ny=node.y+dirY[i];
                if(nx>=0&&nx<r&&ny>=0&&ny<c){
                    arr[nx][ny]='.';
                    bombCount[nx][ny]=sec+3;
                }
            }
        }
 
    }
 
 
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        r=sc.nextInt();
        c=sc.nextInt();
        n=sc.nextInt();
 
        arr=new char[r][c];
        check=new boolean[r][c];
        bombCount=new int[r][c];
 
        for(int i=0;i<r;i++){
            String input=sc.next();
            for(int j=0;j<c;j++){
                arr[i][j]=input.charAt(j);
                if(arr[i][j]=='O'){
                    bombCount[i][j]=3;
                }
 
            }
        }
        Boom();
 
 
    }
 
 
}
 
 
 
 
 
 
 
 
 
 
 
 
 
cs
728x90
반응형

댓글