๋ฌธ์
์์ ์ง๋
ํ์ด
import java.util.*;
class Solution {
public int solution(int[][] board) {
int answer = 0;
// ํญํ์ ์์น ํ์
ํ๊ธฐ
List<int[]> bomb_location = new ArrayList<>();
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(board[i][j] == 1) bomb_location.add(new int[]{i, j});
}
}
// ํญํ ์ฃผ๋ณ์ ์ํ ํ์ ํ๊ธฐ
for(int i = 0; i < bomb_location.size(); i++) {
int cur_x = bomb_location.get(i)[0];
int cur_y = bomb_location.get(i)[1];
int[] dx = {0, 0, -1, 1, 1, 1, -1, -1};
int[] dy = {1, -1, 0, 0, 1, -1, -1, 1};
int limit = board.length;
for(int j = 0; j < 8; j++) {
int nx = cur_x + dx[j];
int ny = cur_y + dy[j];
if(nx >= 0 && ny >= 0 && nx < limit && ny < limit && board[nx][ny] == 0) {
board[nx][ny] = 2;
}
}
}
// ์์ ์ง๋(0) ๊ฐ์ ์ธ๊ธฐ
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board.length; j++) {
if(board[i][j] == 0) answer++;
}
}
return answer;
}
}
- if(nx >= 0 && ny >= 0 && nx < limit && ny < limit && board[nx][ny] == 0) ์กฐ๊ฑด์์ board[nx][ny]๋ฅผ ์ ์ผ ์์ ์์ฑํ์ฌ, ArrayIndexOutOfBoundsException๊ฐ ๋ฐ์ํ์์ต๋๋ค.
- ArrayIndexOutOfBoundsException : ์ ํด์ง ๋ฐฐ์ด์ ํฌ๊ธฐ๋ณด๋ค ํฌ๊ฑฐ๋ ์์ index์ ๋ํ ์์ฒญ์ด ์์ผ๋ฉด ArrayIndexOutOfBoundsException์ด ๋ฐ์ํฉ๋๋ค.