-
[Day4] 529. Minesweeper@StudY/.algorithm 2019. 2. 18. 11:30
529. Minesweeper
class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
int ci = click[0];
int cj = click[1];
if (board[ci][cj]=='M') {
board[ci][cj]='X';
return board;
} else {
int[] i = {-1,-1,-1, 0,0, 1,1,1};
int[] j = {-1, 0, 1,-1,1,-1,0,1};
int adjacents = 0;
for (int k=0; k<8; k++) {
if (ci+i[k]<0 || ci+i[k]>=board.length) continue;
if (cj+j[k]<0 || cj+j[k]>=board[0].length) continue;
if (board[ci+i[k]][cj+j[k]]=='M') {
adjacents++;
}
}
if (adjacents==0) {
board[ci][cj]='B';
for (int k=0; k<8; k++) {
if (ci+i[k]<0 || ci+i[k]>=board.length) continue;
if (cj+j[k]<0 || cj+j[k]>=board[0].length) continue;
if (board[ci+i[k]][cj+j[k]] == 'E') updateBoard(board, new int[] {ci+i[k], cj+j[k]});
}
return board;
} else {
board[ci][cj]=(char)(adjacents+'0');
}
}
return board;
}
}
Accepted 3 ms 39.1 MB java '@StudY > .algorithm' 카테고리의 다른 글
[Day7] 876. Middle of the Linked List / 86. Partition List (LeetCode) (0) 2019.02.22 [Day6] 981. Time Based Key-Value Store (0) 2019.02.20 [Day3] 108. Convert Sorted Array to Binary Search Tree (0) 2019.02.15 [Day2] 617. Merge Two Binary Trees/ 888. Fair Candy Swap (LeetCode) (0) 2019.02.14 [Day1] 319. Bulb Switcher (LeetCode) (0) 2019.02.12 댓글