-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP130.cpp
75 lines (71 loc) · 2.07 KB
/
P130.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
74
75
#include "header.h"
#include <functional>
#include <vector>
class Solution {
public:
int top,bot,left,right;
inline bool inside(int x, int y) {
if (x>=top && x<=bot && y>=left && y<=right) return true;
return false;
}
inline bool onEdge(int x, int y) {
if (x==top || x==bot || y==left || y==right) return true;
return false;
}
int *s;
int findx(int x) {
if (s[x]!=x) {
if (s[x]<0) return x;
else s[x] = findx(s[x]);
}
return s[x];
}
void solve(vector<vector<char>>& board) {
int r,c;
r = board.size();
if (r==0) return;
c = board[0].size();
if (c==0) return;
top = left = 0;
bot = r-1;
right = c-1;
s = new int[r*c];
for (int i=0; i<r*c; i++) s[i] = i;
for (int i=0; i<r; i++)
for (int j=0; j<c; j++) {
if (board[i][j] == 'X') continue;
int x,y;
x = i+1; y = j;
if (inside(x, y) && board[x][y]=='O') {
s[findx(i*c+j)]=s[findx(x*c+y)];
}
x = i; y = j+1;
if (inside(x, y) && board[x][y]=='O') {
s[findx(i*c+j)]=s[findx(x*c+y)];
}
}
for (int i=0; i<r; i++) {
if (board[i][left]=='O') s[findx(i*c+left)] = -1;
if (board[i][right] == 'O') s[findx(i*c+right)] = -1;
}
for (int j=0; j<c; j++) {
if (board[top][j] == 'O') s[findx(top*c+j)] = -1;
if (board[bot][j] == 'O') s[findx(bot*c+j)] = -1;
}
for (int i=0; i<r; i++)
for (int j=0; j<c; j++) {
int t = i*c+j;
if (s[findx(t)]!=-1) board[i][j]='X';
}
}
};
int main() {
vector<vector<char>> m = {{'X','X','X','X'},{'X','O','O','X'},{'X','X','O','X'},{'X','O','X','X'}};
Solution().solve(m);
for (auto &line:m) {
for (auto &c:line)
cout << c << " ";
cout << endl;
}
return 0;
}