@@ -10,34 +10,42 @@ class Solution {
10
10
11
11
queue<pair<int , int >> q;
12
12
for (int i = 0 ; i < board.size (); ++i) {
13
- q.emplace (i, 0 );
14
- q.emplace (i, board[0 ].size () - 1 );
15
- }
16
- for (int j = 0 ; j < board[0 ].size (); ++j) {
17
- q.emplace (0 , j);
18
- q.emplace (board.size () - 1 , j);
13
+ if (board[i][0 ] == ' O' ) {
14
+ board[i][0 ] = ' V' ;
15
+ q.emplace (i, 0 );
16
+ }
17
+ if (board[i][board[0 ].size () - 1 ] == ' O' ) {
18
+ board[i][board[0 ].size () - 1 ] = ' V' ;
19
+ q.emplace (i, board[0 ].size () - 1 );
20
+ }
19
21
}
20
22
23
+ for (int j = 1 ; j < board[0 ].size () - 1 ; ++j) {
24
+ if (board[0 ][j] == ' O' ) {
25
+ board[0 ][j] = ' V' ;
26
+ q.emplace (0 , j);
27
+ }
28
+ if (board[board.size () - 1 ][j] == ' O' ) {
29
+ board[board.size () - 1 ][j] = ' V' ;
30
+ q.emplace (board.size () - 1 , j);
31
+ }
32
+ }
21
33
while (!q.empty ()) {
22
34
int i, j;
23
35
tie (i, j) = q.front ();
24
36
q.pop ();
25
- if (board[i][j] == ' O' || board[i][j] == ' V' ) {
26
- board[i][j] = ' V' ;
27
- const vector<pair<int , int >> directions{{0 , -1 }, {0 , 1 },
28
- {-1 , 0 }, {1 , 0 }};
29
- for (const auto & d : directions) {
30
- const int x = i + d.first , y = j + d.second ;
31
- if (0 <= x && x < board.size () &&
32
- 0 <= y && y < board[0 ].size () &&
33
- board[x][y] == ' O' ) {
34
- board[x][y] = ' V' ;
35
- q.emplace (x, y);
36
- }
37
+ static const vector<pair<int , int >> directions{{0 , -1 }, {0 , 1 },
38
+ {-1 , 0 }, {1 , 0 }};
39
+ for (const auto & d : directions) {
40
+ const int x = i + d.first , y = j + d.second ;
41
+ if (0 <= x && x < board.size () &&
42
+ 0 <= y && y < board[0 ].size () &&
43
+ board[x][y] == ' O' ) {
44
+ board[x][y] = ' V' ;
45
+ q.emplace (x, y);
37
46
}
38
47
}
39
48
}
40
-
41
49
for (int i = 0 ; i < board.size (); ++i) {
42
50
for (int j = 0 ; j < board[0 ].size (); ++j) {
43
51
if (board[i][j] != ' V' ) {
0 commit comments