In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

Example

For

matrix = [[true, false, false],
          [false, true, false],
          [false, false, false]]

the output should be

solution(matrix) = [[1, 2, 1],
                    [2, 1, 1],
                    [1, 1, 1]]

Check out the image below for better understanding:

Input/Output

풀이

int[][] solution(boolean[][] matrix) {
    // 결과값을 담을 이중 배열 선언
    int [][] result = new int [matrix.length][matrix[0].length];
    
    for (int i = 0; i < matrix.length; i ++) {
        for (int j = 0; j < matrix[i].length; j ++) {
            // matirx 현재 위치가 true (깃발) 일 때
            if (matrix[i][j]) {
                // 첫 행의 경우
                if (i == 0) {
                    // 첫 열
                    if (j == 0) {
                        result[i][j + 1] += 1;
                        result[i + 1][j] += 1;
                        result[i + 1][j + 1] += 1;
                    } 
                    // 마지막 열
                    else if (j == matrix[i].length - 1) {
                        result[i][j - 1] += 1;
                        result[i + 1][j] += 1;
                        result[i + 1][j - 1] += 1;
                    }
                    // 나머지 열 
                    else {
                        result[i][j + 1] += 1;
                        result[i][j - 1] += 1;
                        
                        result[i + 1][j] += 1;
                        result[i + 1][j + 1] += 1;
                        result[i + 1][j - 1] += 1;
                    }
                } 
                // 마지막 행의 경우
                else if (i == matrix.length - 1) {
                    if (j == 0) {
                        result[i][j + 1] += 1;
                        result[i - 1][j] += 1;
                        result[i - 1][j + 1] += 1;
                    } else if (j == matrix[i].length - 1) {
                        result[i][j - 1] += 1;
                        result[i - 1][j] += 1;
                        result[i - 1][j - 1] += 1;
                    } else {
                        result[i][j + 1] += 1;
                        result[i][j - 1] += 1;
                        
                        result[i - 1][j] += 1;
                        result[i - 1][j + 1] += 1;
                        result[i - 1][j - 1] += 1;
                    }
                }
                // 나머지 행일 경우 
                else {
                    if (j == 0) {
                        result[i][j + 1] += 1;
                        result[i - 1][j] += 1;
                        result[i - 1][j + 1] += 1;
                        result[i + 1][j] += 1;
                        result[i + 1][j + 1] += 1;
                    } else if (j == matrix[i].length - 1) {
                        result[i][j - 1] += 1;
                        result[i - 1][j] += 1;
                        result[i - 1][j - 1] += 1;
                        result[i + 1][j] += 1;
                        result[i + 1][j - 1] += 1;
                    } else {
                        result[i][j + 1] += 1;
                        result[i][j - 1] += 1;
                        
                        result[i - 1][j] += 1;
                        result[i + 1][j] += 1;
                        
                        result[i - 1][j - 1] += 1;
                        result[i + 1][j + 1] += 1;
                        result[i + 1][j - 1] += 1;
                        result[i - 1][j + 1] += 1;
                    }
                }
            }
        }
    }
    
    return result;
}