-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path934.cpp
More file actions
103 lines (99 loc) · 3.46 KB
/
934.cpp
File metadata and controls
103 lines (99 loc) · 3.46 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
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
96
97
98
99
100
101
102
103
__________________________________________________________________________________________________
sample 28 ms submission
static const auto _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
class Solution {
public:
void DFS(vector <vector <int>>& A, queue <int>& to_expand, int x, int y)
{
A[x][y] = 2;
to_expand.push(x * N + y);
for(int i = 0; i < 4; i++)
{
int next_x = x + dx[i], next_y = y + dy[i];
if(0 <= next_x && next_x < N && 0 <= next_y && next_y < N && A[next_x][next_y] == 1)
DFS(A, to_expand, next_x, next_y);
}
return;
}
int shortestBridge(vector<vector<int>>& A)
{
N = A.size();
queue <int> to_expand;
for(int x = 0; x < N && to_expand.empty(); x++)
for(int y = 0; y < N && to_expand.empty(); y++)
if(A[x][y] == 1)
DFS(A, to_expand, x, y);
int distance = 0;
while(to_expand.size() > 0)
{
int num = to_expand.size();
for(int i = 0; i < num; i++)
{
int position = to_expand.front();
to_expand.pop();
int x = position / N, y = position % N;
for(int j = 0; j < 4; j++)
{
int next_x = x + dx[j], next_y = y + dy[j];
if(0 <= next_x && next_x < N && 0 <= next_y && next_y < N)
{
if(A[next_x][next_y] == 1)
return distance;
if(A[next_x][next_y] == 0)
{
A[next_x][next_y] = 2;
to_expand.push(next_x * N + next_y);
}
}
}
}
distance++;
}
return 0;
}
private:
int N, dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
};
__________________________________________________________________________________________________
sample 12936 kb submission
class Solution {
public:
int dsf(vector<vector<int>>& A, int i, int j) {
if (i < 0 || j < 0 ||
i == A.size() || j == A.size() ||
A[i][j] != 1)
return 0;
A[i][j] = 2;
return 1 + dsf(A, i + 1, j) + \
dsf(A, i - 1, j) + \
dsf(A, i, j + 1) + dsf(A, i, j - 1);
}
bool expand(vector<vector<int>>& A, int i, int j, int cl) {
if (i < 0 || j < 0 || i == A.size() || j == A.size())
return false;
if (A[i][j] == 0)
A[i][j] = cl + 1;
return A[i][j] == 1;
}
int shortestBridge(vector<vector<int>>& A) {
for (int i = 0, found = 0; !found && i < A.size(); ++i) {
for (int j = 0; !found && j < A[0].size(); ++j)
found = dsf(A, i, j);
}
for (int cl = 2; ; ++cl)
for (int i = 0; i < A.size(); ++i)
for (int j = 0; j < A.size(); ++j)
if (A[i][j] == cl &&
((expand(A, i - 1, j, cl) ||
expand(A, i, j - 1, cl) ||
expand(A, i + 1, j, cl) ||
expand(A, i, j + 1, cl))))
return cl - 2;
}
};
__________________________________________________________________________________________________