forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
130 lines (106 loc) · 3.6 KB
/
B.cpp
File metadata and controls
130 lines (106 loc) · 3.6 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
double safe_sqrt(double x) {
return sqrt(max(0.0,x));
}
int GI(int& x) {
return scanf("%lld", &x);
}
int n;
char a[44][44];
pair<int,int> from[44][44], to[44][44], rot[44][44];
int res[44][44];
bool inside(int i, int j) {
return i > 0 && i <= n && j > 0 && j <= n;
}
pair<int,int> rotate(int i, int j) {
return make_pair(n+1-j, i);
}
int check() {
memset(res, 0, sizeof res);
// start from vertices with in-degree == 0
FOR(i,1,n) FOR(j,1,n) if (to[i][j].first > 0 && res[i][j] == 0) {
int x = i, y = j;
while (x > 0 && y > 0 && res[x][y] == 0) {
int u = from[x][y].first;
int v = from[x][y].second;
if (u < 0) break;
if (res[u][v]) break;
res[x][y] = 1;
res[u][v] = 2;
x = from[u][v].first;
y = from[u][v].second;
}
}
// start from any vertex
FOR(i,1,n) FOR(j,1,n) if (res[i][j] == 0 && from[i][j].first > 0) {
int x = i, y = j;
while (x > 0 && y > 0 && res[x][y] == 0) {
int u = from[x][y].first;
int v = from[x][y].second;
if (u < 0) break;
if (res[u][v]) break;
if (res[x][y]) break;
res[x][y] = 1;
res[u][v] = 2;
x = from[u][v].first;
y = from[u][v].second;
}
}
FOR(i,1,n) FOR(j,1,n) if (a[i][j] == '1' && res[i][j] == 0) return 0;
return 1;
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
freopen("divide.in", "r", stdin);
freopen("divide.out", "w", stdout);
while (cin >> n) {
FOR(i,1,n) FOR(j,1,n) cin >> a[i][j];
FOR(i,1,n) FOR(j,1,n) rot[i][j] = make_pair(i, j);
try {
REP(r,4) {
FOR(di,-20,20) FOR(dj,-20,20) {
memset(to, -1, sizeof to);
memset(from, -1, sizeof from);
FOR(i,1,n) FOR(j,1,n) if (a[i][j] == '1') {
int u = rot[i][j].first + di;
int v = rot[i][j].second + dj;
if (u == i && v == j) continue;
if (inside(u, v) && a[u][v] == '1') {
from[i][j] = make_pair(u, v);
to[u][v] = make_pair(i, j);
}
}
if (check()) {
throw 1;
}
}
FOR(i,1,n) FOR(j,1,n) rot[i][j] = rotate(rot[i][j].first, rot[i][j].second);
}
cout << "NO" << endl;
} catch (...) {
cout << "YES" << endl;
FOR(i,1,n) {
FOR(j,1,n) {
if (res[i][j] == 1) cout << 1;
else cout << 0;
}
cout << endl;
}
}
}
}