-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24727.cpp
79 lines (74 loc) · 1.82 KB
/
24727.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
#include <bits/stdc++.h>
void solve(){
int N,A,B; std::cin >> N >> A >> B;
int map[N][N]; memset(map, 0, sizeof map);
map[0][0] = 1; A--;
std::queue<std::pair<int,int>> q;
q.push({0,0});
int xxxx[] = {0,-1,0,1};
int yyyy[] = {-1,0,1,0};
while (q.size()) {
auto [x,y] = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + xxxx[k];
int ny = y + yyyy[k];
if (nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
if (!A || map[nx][ny]) continue;
map[nx][ny] = 1;
q.push({nx,ny});
A--;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
bool found = true;
for (int k = 0; k < 4; k++) {
int x = i + xxxx[k];
int y = j + yyyy[k];
if (x < 0 || y < 0 || x >= N || y >= N) {}
else if (!B || map[x][y] == 1 || map[i][j]) found = false;
}
if (found && q.empty()) {
q.push({i,j});
map[i][j] = 2;
B--;
}
}
}
while (q.size()) {
auto [x,y] = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + xxxx[k];
int ny = y + yyyy[k];
if (nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
if (!B || map[nx][ny]) continue;
bool found = true;
for (int i = 0; i < 4; i++) {
int x = nx + xxxx[i];
int y = ny + yyyy[i];
if (x < 0 || y < 0 || x >= N || y >= N) {}
else if (map[x][y] == 1) found = false;
}
if (found) {
map[nx][ny] = 2;
q.push({nx,ny});
B--;
}
}
}
if (A || B) std::cout << -1;
else {
std::cout << 1 << '\n';
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
std::cout << map[i][j];
}
std::cout << '\n';
}
}
}
int main(){
std::cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}