0초) 폭탄 설치
1초 ) 아무것도 하지 않는다
(1) 2초 ) 모든 칸에 폭탄 설치
(2) 3초 ) 0초에 설치한 폭탄이 폭발한다
(1)과 (2)를 반복한다
https://www.acmicpc.net/problem/16918
16918번: 봄버맨
첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다.
www.acmicpc.net
(1) : time이 짝수일 때
(2) : time 이 홀수일 때
---
1. 지금 터질 폭탄에 영향을 주지 않는 방안
- 폭탄 폭발할 때, 지금 터질 폭탄에 영향을 주면 안되므로 나는 배열을 복사했다
- 다른 풀이를 확인해보니 지금 터질 폭탄에 영향을 주지 않게 하기 위해서 상하좌우를 확인할 때 현재 시각과 같지 않다면 폭탄을 터트리도록 하였다. (훨씬 좋은 방법이라 생각된다)
2. 폭탄이 터질 시각을 관리하는 방안
- 폭탄이 터질 시간 = 현재시간 + 3초 후
=> 현재 시간이 폭탄이 터질 시간인지 확인한다
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int R, C, N;
static char[][] box;
static int[][] bombTime;
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());
// 폭탄 -> 3초후에 폭발 & 인접한 네칸도 함께 폭발 & 연쇄 반응 없음
// (i,j) -> (i+1,j), (i-1,j), (i,j+1), (i,j-1)
// 0) 초기 폭탄 설치
// 1) 아무것도 하지 않음
// 2) 모든 칸에 폭탄을 설치함 -> 모든 칸은 폭탄을 가지고 있음
// 3) 폭발
// 2과 3 반복함!
bombTime = new int[R][C];
box = new char[R][C];
for(int r = 0; r < R; r++) {
String line = br.readLine();
for(int c = 0; c < C; c++) {
box[r][c] = line.charAt(c);
if(box[r][c]=='O') {
bombTime[r][c] = 3; // 3초일때 터지게 된다
}
}
}
for(int time = 1; time <= N; time++) {
// 폭탄이 터지는 시간 = 놓인시간 + 3
if(time % 2 == 0) {// 반복하므로
// 모든 칸에 폭탄을 설치함
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
if(bombTime[i][j]==0) {
bombTime[i][j] = time + 3;
}
}
}
}else {
int[][] temp = copy(bombTime);
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
if(bombTime[i][j] == time) {
temp[i][j] = 0;
for(int w = 0; w < 4; w++) {
int nx = i + way[w][0];
int ny = j + way[w][1];
if(nx < 0 || nx >= R || ny < 0 || ny >= C)continue;
temp[nx][ny] = 0;
}
}
}
}
bombTime = copy(temp);
}
}
printBox();
}
static int[][] way = {{-1,0},{1,0},{0,1},{0,-1}};
static int[][] copy(int[][] bombTime){
int[][] temp = new int[R][C];
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
temp[i][j] = bombTime[i][j];
}
}
return temp;
}
static void printTime() {
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
System.out.print(bombTime[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
static void printBox() {
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
if(bombTime[i][j]>0) {
System.out.print("O");
}else {
System.out.print(".");
}
}
System.out.println();
}
System.out.println();
}
}
[BJ] 2638 : 치즈 (0) | 2024.01.08 |
---|---|
[BJ] 13549 : 숨바꼭질3 (0) | 2024.01.02 |
[시뮬레이션] 끌어내리기 (0) | 2023.12.28 |
[BJ] 21609: 상어 중학교 (0) | 2023.12.13 |
[BJ] 1965: 상자넣기 (0) | 2023.12.12 |