문제
https://www.acmicpc.net/problem/16918
16918번: 봄버맨
첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다.
www.acmicpc.net
코드
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
char board[][] = new char [r][c];
for(int i=0;i<r;i++) {
String str = br.readLine();
for(int j=0;j<c;j++) {
board[i][j] = str.charAt(j);
}
}
for(int t=2;t<=n;t++) {
if(t%2 == 0) {
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
if(board[i][j] == 'O') {
board[i][j] = 'X';
}
else {
board[i][j] = 'O';
}
}
}
}
else {
int dx [] = {1, 0, -1, 0};
int dy [] = {0, 1, 0, -1};
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
if(board[i][j] == 'X') {
board[i][j] = '.';
for(int k=0;k<4;k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if(nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
if(board[nx][ny] == 'X') continue;
board[nx][ny] = '.';
}
}
}
}
}
}
if(n%2==0) {
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
System.out.print("O");
}
System.out.println();
}
}
else {
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
}