-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path803A. Maximal Binary Matrix.cpp
68 lines (60 loc) · 1.12 KB
/
803A. Maximal Binary Matrix.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
#include <stdio.h>
using namespace std;
int const N = 101;
bool grid[N][N];
int main() {
int n, k;
scanf("%d%d", &n, &k);
if(n * n < k) {
puts("-1");
} else if(n * n == k) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(j) putchar(' ');
putchar('1');
}
putchar('\n');
}
} else {
int i = 0, j = 0, x = 0, y = 0, c = 0;
while(k > 0) {
if(i == x && j == y) {
grid[i][j] = 1;
--k;
} else {
grid[i][j] = 1;
grid[x][y] = 1;
k -= 2;
}
++j;
++x;
if(k < 0) {
--j;
--x;
grid[i][j] = 0;
grid[x][y] = 0;
++j;
++x;
k += 2;
c++;
i = j = x = y = c;
} else if(j == n && x == n) {
c++;
i = j = x = y = c;
}
}
if(k < 0) {
puts("-1");
return 0;
} else {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(j) putchar(' ');
printf("%d", grid[i][j]);
}
putchar('\n');
}
}
}
return 0;
}