-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurroundedRegion.cpp
More file actions
55 lines (54 loc) · 2.01 KB
/
SurroundedRegion.cpp
File metadata and controls
55 lines (54 loc) · 2.01 KB
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
class Solution {
public:
struct myHash
{
size_t operator()(pair<int, int> val) const
{
string s = to_string(val.first)+","+to_string(val.second);
hash<string> hf;
return hf(s);
}
};
void solve(vector<vector<char>>& board) {
int m = board.size();
if (!m) return;
int n = board[0].size();
unordered_set<pair<int,int>,myHash> upii;
getFreeO(upii,board);
for (int i = 1; i< m ; i++){
for (int j = 1 ; j < n ; j++){
if (board[i][j] =='O' && !upii.count(make_pair(i,j)))board[i][j] = 'X';
}
}
}
void getFreeO(unordered_set<pair<int,int>,myHash>& upii,vector<vector<char>>& board){
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m ;i++){
for (int j = 0 ; j < n ; j++){
if (i*j == 0 || i==m-1 || j == n-1){
if (board[i][j]=='O' && !upii.count(make_pair(i,j))){
queue<pair<int,int>> q;
q.push(make_pair(i,j));
upii.insert(make_pair(i,j));
vector<pair<int,int>> dir = {{0,1},{0,-1},{1,0},{-1,0}};
while(!q.empty()){
auto t = q.front();
q.pop();
for (auto x:dir){
int nx = t.first + x.first;
int ny = t.second + x.second;
if (nx>=0 && nx < m && ny >=0 && ny < n && !upii.count(make_pair(nx,ny))){
if (board[nx][ny] =='O'){
q.push(make_pair(nx,ny));
upii.insert(make_pair(nx,ny));
}
}
}
}
}
}
}
}
}
};