-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Make_Connected.cpp
More file actions
71 lines (60 loc) · 1.49 KB
/
B_Make_Connected.cpp
File metadata and controls
71 lines (60 loc) · 1.49 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
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<string> v(n);
for(int i=0;i<n;i++) cin >> v[i];
int tot = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(v[i][j] == '.') continue;
tot++;
if(i-1 >= 0 && i+1 < n && v[i-1][j] == '#' && v[i+1][j] == '#') {
cout << "NO\n";
return;
}
if(j-1 >= 0 && j+1 < n && v[i][j-1] == '#' && v[i][j+1] == '#') {
cout << "NO\n";
return;
}
}
}
if(tot <= 1) {
cout << "YES\n";
return;
}
for(int i=0;i<n-1;i++) {
for(int j=0;j<n-1;j++) {
if((v[i][j] == '#') + (v[i+1][j] == '#') + (v[i][j+1] == '#') + (v[i+1][j+1] == '#') == tot) {
cout << "YES\n";
return;
}
}
}
int mxz = 0, mnz = 1e9;
int mxf = -1e9, mnf = 1e9;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(v[i][j] == '#') {
mxz = max(mxz, i+j);
mnz = min(mnz, i+j);
mxf = max(mxf, i-j);
mnf = min(mnf, i-j);
}
}
}
if(mxz - mnz <= 1 || mxf - mnf <= 1) cout << "YES\n";
else cout << "NO\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}