class Solution {
public int solution(int[][] board) {
int[][] boomBoard = new int[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 1) checkExplosion(i, j, boomBoard);
}
}
int count = 0;
for (int[] array : boomBoard) {
for (int value : array) {
if (value == 0) count++;
}
}
return count;
}
static void checkExplosion(int x, int y, int[][] boomBoard) {
int explosionX;
int explosionY;
int[] aroundX = {0, -1, -1, -1, 0, 0, 1, 1, 1};
int[] aroundY = {0, -1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 0; i < 9; i++) {
explosionX = x + aroundX[i];
explosionY = y + aroundY[i];
if (explosionX < boomBoard.length && explosionY < boomBoard.length)
if (explosionX >= 0 && explosionY >= 0) boomBoard[explosionX][explosionY] = 1;
}
}
}
- 와.. 이게 1점 짜리 문제네...
- 먼저 위험지대를 표시할 2차배열을 하나 선언한다.
- 2중 반복문을 돌아서 주어진 2차배열을 모두 순회한다.
- 조건문을 넣어 폭탄을 발견시 checkExplosion함수를 호출한다.
- 함수에는 해당 x,y좌표와 위험지대가 표시된 2차배열이 주어진다.
- 그럼 그 것을 기준으로 aroundX를 explosion X에 x와 함께 더하고 y도 같이 해줍니다.
- 그랬을 경우 조건문을 걸어서 더한 값이 2차배열을 넘어서지 않는지 검증해줍니다.
- 넘어서지 않는다면 위험구역으로 바꿔버립니다.
- 반복문이 완료가 되면 위험구역이 표시된 2차배열을 얻을수 있습니다.
- 향상된 포문을 이중으로 돌려서 0의 갯수를 세면 그것이 안전구역의 갯수. 끝.