其他分享
首页 > 其他分享> > 529. Minesweeper

529. Minesweeper

作者:互联网

问题:

扫雷问题:给定一个扫雷地图,

包含:

给出扫雷点击坐标click

求,这次click之后的扫雷地图的状态。

变化规则:

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']]

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']]

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.

  

Example 1:

 

Example 2:

 

 解法:BFS

queue中:从click的cell开始,存入坐标。

♻️ 优化:为了防止重复入队相同的cell,则在第一次入队时,将该cell设为B。

以后只有cell为E的时候,才入队。

但是当前cell为B的时候,有可能其四周埋雷,需要更新为埋雷数量1~8。

 

代码参考:

 1 class Solution {
 2 public:
 3     vector<vector<int>> dir = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
 4     int n,m;
 5     int countM(int i, int j, vector<vector<char>>& board){
 6         int x = 0, y = 0;
 7         int count = 0;
 8         for(auto d:dir) {
 9             x=i+d[0], y=j+d[1];
10             if(x<0 || y<0 || x>=n || y>=m) continue;
11             if(board[x][y]=='M' || board[x][y]=='X')count++;
12         }
13         return count;
14     }
15     vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
16         n = board.size();
17         m = board[0].size();
18         if(click[0]<0 || click[1]<0 || click[0]>=n || click[1]>=m) return board;
19         /*vector<vector<int>> markboard(n, vector<int>(m,0));
20         for(int i=0; i<n; i++) {
21             for(int j=0; j<m; j++) {
22                 if(board[i][j]=='M') markM(i,j,markboard, board);
23             }
24         }
25         for(int i=0; i<n; i++) {
26             for(int j=0; j<m; j++) {
27                 printf("%d ", markboard[i][j]);
28             }
29             printf("\n");
30         }*/
31         queue<pair<int, int>> q;
32         q.push({click[0],click[1]});
33         int cur_x, cur_y, x, y;
34         while(!q.empty()) {
35             int sz = q.size();
36             for(int i=0; i<sz; i++) {
37                 cur_x = q.front().first;
38                 cur_y = q.front().second;
39                 q.pop();
40                 if(board[cur_x][cur_y]=='M') {
41                     board[cur_x][cur_y]='X';
42                     continue;
43                 }
44                 int cM = countM(cur_x,cur_y,board);
45                 if(cM>0) {
46                     board[cur_x][cur_y]=cM+'0';
47                     continue;
48                 }
49                 board[cur_x][cur_y]='B';
50                 for(auto d:dir) {
51                     x = cur_x+d[0], y = cur_y+d[1];
52                     //printf("x:%d, y:%d\n",x,y);
53                     if(x<0 || y<0 || x>=n || y>=m || board[x][y]!='E') continue;
54                     //printf("inqueue:(%d,%d)=%c\n",x,y,board[x][y]);
55                     q.push({x,y});
56                     board[x][y]='B';
57                     //优化,第一次判断立即将该点设为非E,之后其他方向点判断时,直接continue
58                     //不用加入queue.保证一个cell只加入queue一次。
59                     
60                 }
61             }
62         }
63         return board;
64     }
65 };

 

标签:cur,int,Minesweeper,529,cell,埋雷,board,click
来源: https://www.cnblogs.com/habibah-chang/p/14476299.html