Skip to content

Commit 975e70f

Browse files
authored
Create 529.Minesweeper.cpp
1 parent dd818a5 commit 975e70f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Solution {
2+
public:
3+
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
4+
{
5+
int M = board.size();
6+
int N = board[0].size();
7+
if (board[click[0]][click[1]]=='M')
8+
{
9+
board[click[0]][click[1]] = 'X';
10+
return board;
11+
}
12+
13+
auto dir = vector<pair<int,int>>({{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}});
14+
set<pair<int,int>>visited;
15+
16+
queue<pair<int,int>>q;
17+
q.push({click[0],click[1]});
18+
visited.insert({click[0],click[1]});
19+
20+
while (!q.empty())
21+
{
22+
int i = q.front().first;
23+
int j = q.front().second;
24+
q.pop();
25+
26+
vector<pair<int,int>>next;
27+
int count = 0;
28+
for (int k=0; k<8; k++)
29+
{
30+
int x = i+dir[k].first;
31+
int y = j+dir[k].second;
32+
if (x<0||x>=M||y<0||y>=N) continue;
33+
if (board[x][y]!='E' && board[x][y]!='M') continue;
34+
35+
if (board[x][y]=='M') count++;
36+
else if (visited.find({x,y})==visited.end())
37+
next.push_back({x,y});
38+
}
39+
40+
if (count>0)
41+
board[i][j]=count+'0';
42+
else
43+
{
44+
board[i][j]='B';
45+
for (auto p:next)
46+
{
47+
q.push(p);
48+
visited.insert(p);
49+
}
50+
}
51+
}
52+
53+
return board;
54+
}
55+
};

0 commit comments

Comments
 (0)