|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +public class Main { |
| 4 | + public static void main(String[] args) { |
| 5 | + Scanner scanner = new Scanner(System.in); |
| 6 | + |
| 7 | + int n = scanner.nextInt(); |
| 8 | + scanner.nextLine(); // 버퍼 비우기 |
| 9 | + |
| 10 | + // 입력받기 |
| 11 | + char[][] originalGrid = new char[n][n]; // 지뢰 위치 |
| 12 | + char[][] transformedGrid = new char[n][n]; // 플레이어가 연 칸 |
| 13 | + char[][] resultGrid = new char[n][n]; // 최종 출력 결과 |
| 14 | + |
| 15 | + // 지뢰 위치 입력 |
| 16 | + for (int i = 0; i < n; i++) { |
| 17 | + originalGrid[i] = scanner.nextLine().toCharArray(); |
| 18 | + } |
| 19 | + |
| 20 | + // 플레이어가 연 칸 입력 |
| 21 | + for (int i = 0; i < n; i++) { |
| 22 | + transformedGrid[i] = scanner.nextLine().toCharArray(); |
| 23 | + } |
| 24 | + |
| 25 | + // 결과 배열 초기화 |
| 26 | + for (int i = 0; i < n; i++) { |
| 27 | + for (int j = 0; j < n; j++) { |
| 28 | + resultGrid[i][j] = '.'; // 기본 값으로 초기화 |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + boolean isGameOver = false; |
| 33 | + |
| 34 | + // 변환된 grid 처리 |
| 35 | + for (int i = 0; i < n; i++) { |
| 36 | + for (int j = 0; j < n; j++) { |
| 37 | + if (transformedGrid[i][j] == 'x') { // 클릭한 칸 |
| 38 | + if (originalGrid[i][j] == '*') { // 지뢰를 클릭한 경우 |
| 39 | + isGameOver = true; // 게임 종료 조건 |
| 40 | + } else { |
| 41 | + // 주변 지뢰 개수를 계산하여 표시 |
| 42 | + resultGrid[i][j] = (char) (countMines(i, j, n, originalGrid) + '0'); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // 게임 종료 시 모든 지뢰 표시 |
| 49 | + if (isGameOver) { |
| 50 | + for (int i = 0; i < n; i++) { |
| 51 | + for (int j = 0; j < n; j++) { |
| 52 | + if (originalGrid[i][j] == '*') { |
| 53 | + resultGrid[i][j] = '*'; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // 결과 출력 |
| 60 | + for (char[] row : resultGrid) { |
| 61 | + for (char ch : row) { |
| 62 | + System.out.print(ch); |
| 63 | + } |
| 64 | + System.out.println(); |
| 65 | + } |
| 66 | + |
| 67 | + scanner.close(); |
| 68 | + } |
| 69 | + |
| 70 | + // 주변 8칸의 지뢰 개수를 세는 함수 |
| 71 | + public static int countMines(int x, int y, int n, char[][] grid) { |
| 72 | + int count = 0; |
| 73 | + for (int i = x - 1; i <= x + 1; i++) { |
| 74 | + for (int j = y - 1; j <= y + 1; j++) { |
| 75 | + if (i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == '*') { |
| 76 | + count++; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return count; |
| 81 | + } |
| 82 | +} |
0 commit comments