Skip to content

Commit aa91dc4

Browse files
committed
529 测试工具头
1 parent 0e78e52 commit aa91dc4

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Leetcode刷题记录
2323
+ No.95 [cpp](cpp/95.cpp)
2424
+ No.77 [python](python/77.py)
2525
+ No.97 [python](python/97.py)
26+
+ No.529 [cpp](cpp/529.cpp)
2627
+ No.733 [cpp](cpp/733.cpp)
2728

2829
### 并查集

cpp/529.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "tools.h"
2+
#include <bits/stdc++.h>
3+
4+
5+
using namespace std;
6+
7+
#define ADD(a, b) pair<int, int>(a.first + b.first, a.second + b.second)
8+
9+
class Solution
10+
{
11+
public:
12+
vector<vector<char>> updateBoard(vector<vector<char>> &board, vector<int> &click)
13+
{
14+
int x = click[0], y = click[1];
15+
if (board[x][y] == 'M') {
16+
board[x][y] = 'X';
17+
return board;
18+
}
19+
queue<pair<int, int>> q;
20+
int m = board.size(), n = board[0].size();
21+
vector<pair<int, int>> poss = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
22+
vector<vector<bool>> flag(m, vector<bool>(n, false));
23+
q.push({x, y});
24+
flag[x][y] = true;
25+
int next_x, next_y, count;
26+
while (!q.empty())
27+
{
28+
pair<int, int> c = q.front();
29+
q.pop();
30+
x = c.first;
31+
y = c.second;
32+
count = 0;
33+
for (const pair<int, int> &p : poss)
34+
{
35+
next_x = p.first + x;
36+
next_y = p.second + y;
37+
if (check(next_x, next_y, m, n)) {
38+
if (board[next_x][next_y] == 'M') ++count;
39+
}
40+
}
41+
// printf("%d, %d, %d\n", next_x, next_y, count);
42+
if (count==0) board[x][y] = 'B';
43+
else board[x][y] = static_cast<char>(count + '0');
44+
if (board[x][y] == 'B')
45+
{
46+
for (const pair<int, int> &p : poss)
47+
{
48+
next_x = p.first + x;
49+
next_y = p.second + y;
50+
51+
if (check(next_x, next_y, m, n) && board[next_x][next_y] == 'E' && flag[next_x][next_y]==false)
52+
{
53+
q.push({next_x, next_y});
54+
flag[next_x][next_y] = true;
55+
}
56+
}
57+
}
58+
}
59+
return board;
60+
}
61+
62+
inline bool check(int x, int y, int m, int n)
63+
{
64+
return x >= 0 && x < m && y >= 0 && y < n;
65+
}
66+
};
67+
68+
69+
int main(int argc, char const *argv[])
70+
{
71+
int m, n, x, y;
72+
char c;
73+
Solution s;
74+
freopen("test.in", "r", stdin);
75+
cin >> m >> n;
76+
vector<vector<char>> matrix(m, vector<char>(n));
77+
for (int i = 0; i < m; ++i) {
78+
for (int j = 0; j < n; ++j) {
79+
cin >> c;
80+
matrix[i][j] = c;
81+
}
82+
// PRINT_VECTOR(matrix[i], char);
83+
}
84+
cin >> x >> y;
85+
vector<int> begin = {x, y};
86+
auto res = s.updateBoard(matrix, begin);
87+
PRINT_MATRIX(res, char, cout);
88+
return 0;
89+
}

cpp/tools.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/**
3+
* 用作Leetcode 测试用的宏
4+
*
5+
* 测试容器
6+
* + PRINT_VRECTOR 用于打印整个线性容器的一个宏 (之后可以会拓展支持数组)
7+
* + PRINT_MATRIX 用于打印二阶vec的宏 (现在只能使用vec, 之后可能扩展为arr)
8+
*/
9+
10+
#define PRINT_VECTOR(vec, type, io_obj) \
11+
do { \
12+
copy(vec.begin(), vec.end(), ostream_iterator<type>(io_obj, " ")); \
13+
cout << endl; \
14+
} while(0)
15+
16+
#define PRINT_MATRIX(matrix, type, io_obj) \
17+
do { \
18+
for (auto &vec: matrix) {\
19+
PRINT_VECTOR(vec, type, io_obj); \
20+
} \
21+
} while(0)
22+
23+
24+
#define CHANGE_()

0 commit comments

Comments
 (0)