@@ -5,63 +5,77 @@ CS203_DSAA_template
55Copyright (C) 2022-2023 nanoseeds
66
77*/
8- #include " leetcode_1020_test.hpp"
8+ #ifdef CS203_DSAA_TEST_MACRO
9+
10+ #include < cstdint>
11+ #include < cstddef>
12+ #include < vector>
913
1014namespace leetcode_1020 {
11- void visit (const vector<vector<int32_t >> &grid, vector<vector<uint8_t >> &visited, size_t row, size_t col) {
12- const auto judge = [& grid, &visited](size_t r, size_t c) {
13- return grid[r][c] == 1 && visited[r][c] == 0 ;
14- };
15- if (!judge (row, col)) {
16- return ;
17- }
18- const auto row_size{grid.size ()}, col_size{grid.front ().size ()};
19- for (std::vector<std::pair<size_t , size_t >> now{{row, col}}, next{}; !now.empty (); next.clear ()) {
20- for (const auto &[this_row, this_col]: now) {
21- visited[this_row][this_col] = 1 ;
22- if (this_col > 0 && judge (this_row, this_col - 1 )) {
23- next.emplace_back (this_row, this_col - 1 );
24- }
25- if (this_row > 0 && judge (this_row - 1 , this_col)) {
26- next.emplace_back (this_row - 1 , this_col);
27- }
28- if (this_col + 1 < col_size && judge (this_row, this_col + 1 )) {
29- next.emplace_back (this_row, this_col + 1 );
30- }
31- if (this_row + 1 < row_size && judge (this_row + 1 , this_col)) {
32- next.emplace_back (this_row + 1 , this_col);
15+ using std::vector;
16+ using std::size_t ;
17+ #endif
18+
19+ class Solution {
20+ private:
21+ void visit (const vector<vector<int32_t >> &grid, vector<vector<uint8_t >> &visited, size_t row, size_t col) {
22+ const auto judge = [& grid, &visited](size_t r, size_t c) {
23+ return grid[r][c] == 1 && visited[r][c] == 0 ;
24+ };
25+ if (!judge (row, col)) {
26+ return ;
27+ }
28+ const auto row_size{grid.size ()}, col_size{grid.front ().size ()};
29+ for (std::vector<std::pair<size_t , size_t >> now{{row, col}}, next{}; !now.empty (); next.clear ()) {
30+ for (const auto &[this_row, this_col]: now) {
31+ visited[this_row][this_col] = 1 ;
32+ if (this_col > 0 && judge (this_row, this_col - 1 )) {
33+ next.emplace_back (this_row, this_col - 1 );
34+ }
35+ if (this_row > 0 && judge (this_row - 1 , this_col)) {
36+ next.emplace_back (this_row - 1 , this_col);
37+ }
38+ if (this_col + 1 < col_size && judge (this_row, this_col + 1 )) {
39+ next.emplace_back (this_row, this_col + 1 );
40+ }
41+ if (this_row + 1 < row_size && judge (this_row + 1 , this_col)) {
42+ next.emplace_back (this_row + 1 , this_col);
43+ }
3344 }
45+ std::swap (now, next);
3446 }
35- std::swap (now, next);
3647 }
37- }
3848
39- int32_t leetcode_1020::numEnclaves (const vector<vector<int32_t >> &grid) {
40- if (grid.empty () || grid.front ().empty ()) {
41- return 0 ;
42- }
43- const auto row{grid.size ()}, col{grid.front ().size ()};
44- vector<vector<uint8_t >> visited (row, vector<uint8_t >(col, 0 ));
45- int32_t ones{0 };
46- for (const auto &rows: grid) {
47- for (const auto &num: rows) {
48- ones += (num == 1 );
49+ public:
50+ int32_t numEnclaves (const vector<vector<int32_t >> &grid) {
51+ if (grid.empty () || grid.front ().empty ()) {
52+ return 0 ;
4953 }
50- }
51- for (size_t i{0 }; i < col; ++i) {
52- visit (grid, visited, 0 , i);
53- visit (grid, visited, row - 1 , i);
54- }
55- for (size_t i{1 }; i + 1 < row; ++i) {
56- visit (grid, visited, i, 0 );
57- visit (grid, visited, i, col - 1 );
58- }
59- for (const auto &rows: visited) {
60- for (const auto &num: rows) {
61- ones -= (num == 1 );
54+ const auto row{grid.size ()}, col{grid.front ().size ()};
55+ vector<vector<uint8_t >> visited (row, vector<uint8_t >(col, 0 ));
56+ int32_t ones{0 };
57+ for (const auto &rows: grid) {
58+ for (const auto &num: rows) {
59+ ones += (num == 1 );
60+ }
61+ }
62+ for (size_t i{0 }; i < col; ++i) {
63+ visit (grid, visited, 0 , i);
64+ visit (grid, visited, row - 1 , i);
6265 }
66+ for (size_t i{1 }; i + 1 < row; ++i) {
67+ visit (grid, visited, i, 0 );
68+ visit (grid, visited, i, col - 1 );
69+ }
70+ for (const auto &rows: visited) {
71+ for (const auto &num: rows) {
72+ ones -= (num == 1 );
73+ }
74+ }
75+ return ones;
6376 }
64- return ones;
65- }
77+ };
6678
79+ #ifdef CS203_DSAA_TEST_MACRO
6780}
81+ #endif
0 commit comments