반응형

 

[광고 누르면 오늘의 행운 상승!!]

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

 

16918번: 봄버맨

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

www.acmicpc.net

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);
			}
		}
		
	}
}
반응형

+ Recent posts