Created
May 23, 2018 15:09
-
-
Save s4553711/5a25cc126bc353dbbde20544a511da56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<bool>> visited; | |
int n, m; | |
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) { | |
int x, y; | |
n = board.size(); | |
m = board[0].size(); | |
visited.assign(n, vector<bool>(m, false)); | |
for(int i = 0; i < click.size(); i+=2) { | |
x = click[i]; | |
y = click[i + 1]; | |
dfs(board, x, y); | |
} | |
return board; | |
} | |
void dfs(vector<vector<char>>& board, int x, int y) { | |
if (board[x][y] == 'M') { | |
board[x][y] = 'X'; | |
return; | |
} | |
if (board[x][y] != 'E') { | |
return; | |
} | |
int offsetX[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; | |
int offsetY[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; | |
int mines = 0; | |
visited[x][y] = true; | |
for(int i = 0; i < 8; i++) { | |
int row = x + offsetX[i]; | |
int col = y + offsetY[i]; | |
if (row < 0 || row >= n || col < 0 || col >= m) | |
continue; | |
if (board[row][col] == 'M') | |
mines++; | |
} | |
if (mines > 0) { | |
board[x][y] = '0' + mines; | |
} else { | |
board[x][y] = 'B'; | |
for(int i = 0; i < 8; i++) { | |
int row = x + offsetX[i]; | |
int col = y + offsetY[i]; | |
if (row < 0 || row >= n || col < 0 || col >= m) continue; | |
dfs(board, row, col); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment