-
Notifications
You must be signed in to change notification settings - Fork 0
/
130.cpp
95 lines (83 loc) · 2.6 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//把所有外部的节点搞成一个,这是个小创新
class Solution {
public:
int* a;
void un(int i, int j)
{
//a[j] = fin(i);
a[fin(j)] = fin(i);
}
int fin(int i)
{
while(a[i] != i)
{
a[i] = a[a[i]];
i = a[i];
}
return i;
}
void solve(vector<vector<char>>& board) {
int m = board.size();
if(m == 0)return;
int n = board[0].size();
if(n == 0)return;
//cout << m << "|" << n << endl;
a = new int[m*n+1];
//a[0] = 0;
//要全部初始化才行
for(int i = 0; i <= m*n; ++i)
a[i] = i;
//刚开始想跑两遍矩阵处理完,现在看能不能跑一遍处理完
//搞不好最后一个点才通到外边,没法一遍确认
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(board[i][j] == 'O')
{
//cout << i << "|" << j << endl;
if(i == 0 || i == m-1 || j == 0 || j == n-1)
{
//算对应坐标,要两个轴都+1才行---都搞错了
un(0, i*n + (j+1));
//cout << "un:" << 0 << "," << i*m + (j+1) << "|";
}
//少写后边的条件了
if(i > 0 && board[i-1][j] == 'O')
{
un((i-1)*n + (j+1), i*n + (j+1));
//cout << "un:" << (i-1)*m + (j+1) << "," << i*m + (j+1) << "|";
}
if(j > 0 && board[i][j-1] == 'O')
{
un(i*n + j, i*n + j+1);
//cout << "un:" << i*m + j << "," << i*m + (j+1) << "|";
}
//cout << endl;
}
}
}
cout << a[0] << endl;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
cout << a[i*n + (j+1)] << "|";
}
cout << endl;
}
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(board[i][j] == 'O')
{
if( fin(0) != fin(i*n + (j+1)) )
{
board[i][j] = 'X';
}
}
}
}
}
};