-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_130.cpp
70 lines (70 loc) · 1.78 KB
/
LC_130.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution {
public:
void bfs(vector<vector<char>>& board,queue<pair<int,int>>& q)
{
pair<int,int>fron;
int i,x1,y1;
vector<vector<int>>dir = {{1,0},{-1,0},{0,1},{0,-1}};
while(!q.empty())
{
fron=q.front();
q.pop();
for(i=0;i<4;i++)
{
x1=fron.first + dir[i][0];
y1=fron.second + dir[i][1];
if((x1>=0) && (x1<board.size()) && (y1>=0) && (y1<board[0].size()) && (board[x1][y1]=='O'))
{
board[x1][y1]='B';
q.push(make_pair(x1,y1));
}
}
}
}
void solve(vector<vector<char>>& board) {
int sz=board.size();
if(sz==0)
return ;
int i,j,sz1=board[0].size();
if(sz1==0)
return;
queue<pair<int,int>>q;
for(i=0;i<sz;i++)
{
if(board[i][0]=='O')
{
q.push(make_pair(i,0));
board[i][0]='B';
}
if(board[i][sz1-1]=='O')
{
q.push(make_pair(i,sz1-1));
board[i][sz1-1]='B';
}
}
for(i=0;i<sz1;i++)
{
if(board[0][i]=='O')
{
q.push(make_pair(0,i));
board[0][i]='B';
}
if(board[sz-1][i]=='O')
{
q.push(make_pair(sz-1,i));
board[sz-1][i]='B';
}
}
bfs(board,q);
for(i=0;i<sz;i++)
{
for(j=0;j<sz1;j++)
{
if(board[i][j]=='B')
board[i][j]='O';
else
board[i][j]='X';
}
}
}
};