-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
73 lines (70 loc) · 1.83 KB
/
solution.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
71
72
73
class Solution
{
public:
void process(int i,int j,vector<vector<char> >& board)
{
int m=board.size();
int n=board[0].size();
typedef pair<int,int> point;
queue<point> Q;
Q.push(point(i,j));
board[i][j]='E';
while(!Q.empty())
{
point tmp=Q.front();
Q.pop();
int x=tmp.first,y=tmp.second;
//extending
if (x!=0&&board[x-1][y]=='O')
{
Q.push(point(x-1,y));
board[x-1][y]='E'; //extended;
}
if (x!=m-1&&board[x+1][y]=='O')
{
Q.push(point(x+1,y));
board[x+1][y]='E'; //extended;
}
if (y!=0&&board[x][y-1]=='O')
{
Q.push(point(x,y-1));
board[x][y-1]='E'; //extended;
}
if (y!=n-1&&board[x][y+1]=='O')
{
Q.push(point(x,y+1));
board[x][y+1]='E'; //extended;
}
}
}
void solve(vector<vector<char> >& board)
{
int m=board.size();
if (m==0)
return;
int n=board[0].size();
int i,j;
for(i=0;i<m;i++)
{
if (board[i][0]=='O')
process(i,0,board);
if (board[i][n-1]=='O')
process(i,n-1,board);
}
for(j=0;j<n;j++)
{
if (board[0][j]=='O')
process(0,j,board);
if (board[m-1][j]=='O')
process(m-1,j,board);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(board[i][j]=='O')
board[i][j]='X';
else if (board[i][j]=='E')
board[i][j]='O';
}
}
};