https://leetcode.com/problems/minesweeper/description/
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int r = click[0], c = click[1];
if(board[r][c] == 'M') {
board[r][c] = 'X';
return board;
}
int NI = board.size(), NJ = board[0].size();
stack<vector<int>> st;
st.push(click);
vector<int> cur;
while(!st.empty()) {
cur = st.top();
st.pop();
r = cur[0], c = cur[1];
int m = 0;
for(int i=r-1;i<=r+1;i++)
for(int j=c-1;j<=c+1;j++) {
if(i>=0 && i<NI && j>=0 && j<NJ) m += (board[i][j]=='M');
}
if(m != 0) board[r][c] = '0' + m;
else {
board[r][c] = 'B';
for(int i=r-1;i<=r+1;i++)
for(int j=c-1;j<=c+1;j++) {
if(i>=0 && i<NI && j>=0 && j<NJ && (board[i][j] != 'B'))
st.push({i,j});
}
}
}
return board;
}
No comments:
Post a Comment