题目:
Let’s play the minesweeper game (Wikipedia, online game)!
You are given a 2D char matrix representing the game board.'M'
represents an unrevealed mine,'E'
represents an unrevealed empty square,'B'
represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit (‘1’ to ‘8’) represents how many mines are adjacent to this revealed square,and finally'X'
represents a revealed mine.
Now given the next click position (row and column indices) among all the unrevealed squares ('M'
or'E'
), return the board after revealing this position according to the following rules:
1.If a mine ('M'
) is revealed, then the game is over - change it to'X'
.
2.If an empty square ('E'
) with no adjacent mines is revealed, then change it to revealed blank ('B'
) and all of its adjacent unrevealed squares should be revealed recursively.
3.If an empty square ('E'
) with at least one adjacent mine is revealed, then change it to a digit ('1'
to'8'
) representing the number of adjacent mines.
4.Return the board when no more squares will be revealed.
Example 1:Input: [['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'M', 'E', 'E'], ['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E']] Click : [3,0] Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Explanation:
Example 2:Input: [['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Click : [1,2] Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'X', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Explanation:
Note:The range of the input matrix’s height and width is
[1,50]
.
The click position will only be an unrevealed square ('M'
or'E'
), which also means the input board contains at least one clickable square.
The input board won’t be a stage when game is over (some mines have been revealed).
For simplicity, not mentioned rules should be ignored in this problem. For example, you don’t need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.
解释:
用bfs做,用queue
注意:
如果点到的是数字或者雷,则退出,如果点到的是空的,则递归,用BFS做,需要用一些额外判断避免重复计算。
python代码(100%了耶):
class Solution(object):
def updateBoard(self, board, click):
"""
:type board: List[List[str]]
:type click: List[int]
:rtype: List[List[str]]
"""
w,h=len(board[0]),len(board)
def countBoard(i,j):
count=0
for di in (-1,0,1):
for dj in (-1,0,1):
if not (di==0 and dj==0):
ni,nj=i+di,j+dj
if ni>=0 and ni<h and nj>=0 and nj<w and board[ni][nj]=='M':
count+=1
return str(count) if count else 'B'
cx,cy=click
if board[cx][cy]=='M':
board[cx][cy]='X'
return board
queue=[click]
board[cx][cy]=countBoard(cx,cy)
if board[cx][cy]!='B':
return board
while queue:
i,j=queue.pop(0)
for di in (-1,0,1):
for dj in (-1,0,1):
if not (di==0 and dj==0):
ni,nj=i+di,j+dj
if ni>=0 and ni<h and nj>=0 and nj<w and board[ni][nj]=='E':
board[ni][nj]=countBoard(ni,nj)
if board[ni][nj]=='B':
queue.append((ni,nj))
return board
c++代码:
#include <queue>
using namespace std;
class Solution {
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int count=0;
int m=board.size(),n=board[0].size();
int x=click[0],y=click[1];
if (board[x][y]=='M')
{
board[x][y]='X';
return board;
}
board[x][y]=countBoard(board,x,y);
if (board[x][y]!='B')
return board;
queue<vector<int>> _queue;
_queue.push(click);
while (!_queue.empty())
{
vector<int> tmp=_queue.front();
int i=tmp[0];
int j= tmp[1];
_queue.pop();
for(int di=-1;di<=1;di++)
{
for(int dj=-1;dj<=1;dj++)
{
if (!(di==0 && dj==0))
{
int ni=i+di,nj=j+dj;
if(ni>=0 && nj>=0 && ni<m && nj<n && board[ni][nj]=='E')
{
board[ni][nj]=countBoard(board,ni,nj);
if (board[ni][nj]=='B')
{
_queue.push({ni,nj});
}
}
}
}
}
}
return board;
}
char countBoard(vector<vector<char>>& board,int i,int j)
{
int count=0;
int m=board.size(),n=board[0].size();
for(int di=-1;di<=1;di++)
{
for(int dj=-1;dj<=1;dj++)
{
if (!(di==0 && dj==0))
{
int ni=i+di,nj=j+dj;
if(ni>=0 && nj>=0 && ni<m && nj<n && board[ni][nj]=='M')
count++;
}
}
}
return count?(count+'0'):'B';
}
};
总结:
感觉bfsd的题目比dfs的题目要少很多,这个当翻开的位置为空的时候可以用bfs继续翻开它周围的点。