[ Solution ]
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
char [][] bomb = new char[n][n];
char [][] map = new char[n][n];
int [] dirx = {-1, -1, -1, 0, 0, 1, 1, 1};
int [] diry = {-1, 0, 1, -1, 1, -1, 0, 1};
int key = 1;
// 지뢰 위치 입력 받기
for(int i = 0; i < n ; i++){
String x = br.readLine();
for(int j = 0; j < n; j++) {
bomb[i][j] = x.charAt(j);
}
}
for(int i = 0; i < n ; i++){
String x = br.readLine();
for(int j = 0; j < n; j++) {
if(x.charAt(j) == 'x') { // 지뢰를 열었을 때
if(bomb[i][j] == '*') { // 출력시 지뢰 부분을 *로 바꾸기 위한 표시
key = 0;
}
else { // 지뢰를 열지 않았을 때
int count = 0;
for (int k = 0; k < 8; k++) { // 해당 칸을 둘러싼 8칸 검사
int newx = i + dirx[k];
int newy = j + diry[k];
if (newx >= 0 && newx < n && newy >= 0 && newy < n && bomb[newx][newy] == '*') // 둘러싼 칸에서 지뢰 있으면 count 1 증가
count++;
}
map[i][j] = (char) (count + '0');
}
}
else {
map[i][j] = '.';
}
}
}
if(key == 0) { // 지뢰 열었을 경우 지뢰 부분 *로 바꾸기
for(int i = 0; i < n ; i++){
for(int j = 0; j < n; j++) {
if(bomb[i][j] == '*')
map[i][j] = '*';
}
}
}
// 전체 출력
for(int i = 0; i < n ; i++){
for(int j = 0; j < n; j++) {
System.out.print(map[i][j]);
}
System.out.print("\n");
}
}
}
평소에 지뢰찾기를 좋아해서 재밌게 풀었던 문제이다.
구현하는 것은 어렵지 않았으나,, 문제 조건을 잘못 이해해서 애를 먹었던 부분이 있었다.
지뢰를 발견했을 때, 지뢰가 있는 부분을 * 로 출력하고 나머지 부분 모두 .으로 출력하는 것으로 풀어서 왜 틀렸는지 한참 찾았다. 앞으로 문제를 잘 이해하고 풀도록 하자,,