50032. N-pieces (special judge)

難度:4.5/5

Second Try: 3/5 Used Time: 17:09

The recursion is prettry easy to maintain and there's not much to be wrong.

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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int n, m, k;
int board[18][18];
int c_cnt[18], r_cnt[18], d1_cnt[35], d2_cnt[35];
void init(){
for(int i = 0; i < 18; i++){
c_cnt[i] = 0;
r_cnt[i] = 0;
}
for(int i = 0; i < 35; i++){
d1_cnt[i] = 0;
d2_cnt[i] = 0;
}
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
board[i][j] = 0;
}
}
void print_board(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 1) printf("o");
else printf(".");
}
printf("\n");
}
}

static inline bool is_valid(int x, int y){
return (c_cnt[x] < k && r_cnt[y] < k &&
d1_cnt[x + y] < k && d2_cnt[n - x + y] < k);
}

bool solve(int x, int y, int cnt){
if(x == n) return false;
if(y == n) x += 1, y = 0;

if(cnt == m){
print_board();
return true;
}

if(is_valid(x, y) == true){
board[x][y] = 1;
c_cnt[x]++, r_cnt[y]++, d1_cnt[x + y]++, d2_cnt[n - x + y]++;

bool ret = solve(x, y + 1, cnt + 1);
if(ret == true) return true;

board[x][y] = 0;
c_cnt[x]--, r_cnt[y]--, d1_cnt[x + y]--, d2_cnt[n - x + y]--;
}

return solve(x, y + 1, cnt);
}

int main(){
while(scanf("%d %d %d", &n, &m, &k) == 3){
init();
if(!solve(0, 0, 0)) printf("N\n");
}
}


50032. N-pieces (special judge)
https://aaronlin1229.github.io/judgegirl_50032/
Author
Akizumi
Posted on
July 17, 2023
Licensed under