반응형
[광고 누르면 오늘의 행운 상승!!]
https://www.acmicpc.net/problem/16918
bfs를 사용한 시뮬레이션 문제
봄버맨 활동, 폭탄 활동 시기를 잘 나누어야 한다.
import java.io.*;
import java.util.*;
class Boom{
int row, col, time=3;
boolean fire;
public Boom(int row, int col) {
this.row = row;
this.col = col;
}
}
public class Main {
public static final int[] dx= {-1,0,1,0};
public static final int[] dy= {0,1,0,-1};
public static Queue<Boom> q;
public static int R,C,N, delay;
public static char[][] map;
public static int[][] boomMap;
public static int boomtime = 3;
public static int time = 0;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
map = new char[R][C];
boomMap = new int[R][C];
q = new LinkedList<>();
for (int i = 0; i < R; i++) {
char ch[] = br.readLine().toCharArray();
for (int j = 0; j < C; j++) {
map[i][j] = ch[j];
if(map[i][j] == 'O') {
q.add(new Boom (i,j));
boomMap[i][j] = boomtime;
//초기 폭탄 설치
}
}
}
bfs();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
public static void bfs() {
while(true) {
time++;
CheckBoom();
CheckBoomMan();
if(time == N) break;
}
}
private static void CheckBoomMan() {
delay++;
if(delay%2 == 0) {
//봄버맨 활동
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if(map[i][j] == '.') {
map[i][j] = 'O';
boomMap[i][j] = boomtime;
q.add(new Boom (i,j));
}
}
}
}
}
private static void CheckBoom() {
int size = q.size();
for (int k = 0; k < size; k++) {
Boom boom = q.poll();
if (boomMap[boom.row][boom.col] == 1) { //1이면 폭발
for (int i = 0; i < 4; i++) {
int nx = boom.row + dx[i];
int ny = boom.col + dy[i];
if (nx < 0 || nx >= R || ny < 0 || ny >= C) {
continue; //선을 넘으면
}
if(boomMap[nx][ny] == boomMap[boom.row][boom.col]) {
continue; //같은시간대에 터지는 친구면
}
map[nx][ny] = '.';
boomMap[nx][ny] = 0;
}
map[boom.row][boom.col] = '.';
boomMap[boom.row][boom.col] = 0;
}
else if(boomMap[boom.row][boom.col]>1){
boomMap[boom.row][boom.col]--; //폭탄맵 갱신
q.add(boom);
}
}
}
}
반응형
'2. 알고리즘사이트 > 1. 백준' 카테고리의 다른 글
양치기 꿍 [백준 3187][실버2][Java] (0) | 2020.03.02 |
---|---|
달이 차오른다, 가자. [백준 1194][골드1][Java] (0) | 2020.03.02 |
색종이 만들기 [백준 2630][실버3][Java] (0) | 2020.03.02 |
보이저 1호 [백준 3987][실버3][Java] (0) | 2020.03.02 |
좋은수열 [백준 2661][골드4][Java] (0) | 2020.03.02 |