扫雷游戏

class Solution {
public:
int cal(int x, int y, vector<vector<char>>& board)
{
int col = board.size();
int row = board[0].size();
int dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int dy[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int count = 0;
for (int i = 0; i != 8; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < col && ny >= 0 && ny < row)
{
if (board[nx][ny] == 'M')
++count;
}
}
return count;
}
void BFSa(vector<vector<char>>& board, int x, int y)
{
int dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int dy[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int col = board.size();
int row = board[0].size();
int count_num = 0;
count_num = cal(x, y, board);
if (count_num == 0)//如果满足条件的话进入
{
board[x][y] = 'B';//相当于visit的条件改变
for (int i = 0; i < 8; ++i)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < col&&ny >= 0 && ny < row)
{
if(board[nx][ny]=='E')//查看visit条件是什么
BFSa(board, nx, ny);
}
}
}
else//不满足条件的话就退出
{
board[x][y] = count_num + '0';
return;
}
}
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
//扫雷游戏和数独游戏差不多的,我们先选择深度优先遍历的算法
int col = board.size();
int row = board[0].size();
int x = click[0];
int y = click[1];
if (board[x][y] == 'M')
{
board[x][y] = 'X';
return board;
}
//如果一开始不是地雷需要宽度优先遍历进行相应的计算
BFSa(board, x, y);
return board;
}
};
