Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3334b04

Browse files
529. Minesweeper
1 parent 7f53234 commit 3334b04

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎minesweeper.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Runtime: 56 ms
2+
// Memory Usage: 19.6 MB
3+
class Solution {
4+
public:
5+
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
6+
queue<pair<int, int> > q;
7+
q.push({click[0], click[1]});
8+
while (!q.empty()) {
9+
auto t = q.front();
10+
q.pop();
11+
12+
int x(t.first), y(t.second);
13+
14+
if (board[x][y] == 'M') {
15+
board[x][y] = 'X';
16+
} else {
17+
vector<vector<int> > directions = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}, { -1, -1}, {1, 1}, { -1, 1}, {1, -1}};
18+
int mines = 0;
19+
for (auto dir : directions) {
20+
int xr = x + dir[0];
21+
int yr = y + dir[1];
22+
23+
if (xr >= 0 && yr >= 0 && xr < board.size() && yr < board[0].size()) {
24+
if (board[xr][yr] == 'X' || board[xr][yr] == 'M') {
25+
mines++;
26+
}
27+
}
28+
}
29+
30+
if (mines > 0) {
31+
board[x][y] = (char)(mines + '0');
32+
} else {
33+
board[x][y] = 'B';
34+
for (auto dir : directions) {
35+
int xr = x + dir[0];
36+
int yr = y + dir[1];
37+
38+
if (xr >= 0 && yr >= 0 && xr < board.size() && yr < board[0].size() && board[xr][yr] == 'E') {
39+
q.push({xr, yr});
40+
board[xr][yr] = 'B';
41+
}
42+
}
43+
}
44+
}
45+
}
46+
return board;
47+
}
48+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /