-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP167.cpp
156 lines (141 loc) · 3.07 KB
/
P167.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <stdio.h>
/*
Define Permutation:
8 queens on board, one in each row/colummn [0;7].
Assume permutation of columns.
Permutation a b c ... means:
column with queen on row a at column 0.
column with queen on row b at column 1.
...
*/
struct PermutationNode {
PermutationNode *next;
int val;
};
struct PermutationHandler {
PermutationNode nodes[9];
void reset(int size) {
for(int i = 0; i < size; ++i) {
nodes[i+1].val = i;
nodes[i].next = &(nodes[i+1]);
}
nodes[size].next = NULL;
}
PermutationNode* root() {
return &(nodes[0]);
}
};
void printPerm(int * const perm, int set = 8) {
std::cerr << " ";
for(int c = 0; c < set; ++c) {
std::cerr << (char)(c+'A');
}
std::cerr << std::endl;
for(int r = 0; r < 8; ++r){
std::cerr << (r+1);
for(int c = 0; c < set; ++c) {
if(perm[c] == r)
std::cerr << "X";
else
std::cerr << "\\";
}
std::cerr << std::endl;
}
}
bool boardOK(int * const perm, int set = 8) {
// simply check diagonals:
bool bL[8], bTL[8], bTR[8], bR[8];
for(int i = 0; i < 8; ++i){
bL[i] = bTL[i] = bTR[i] = bR[i] = false;
}
for(int column = 0; column < set; ++column) {
int row = perm[column];
// check 4 edge positions:
{
// up /:
int columnTop = column + row;
if(columnTop < 8) {
if(bTR[columnTop]) {
return false;
}
bTR[columnTop] = true;
}
else { // hits side:
int rowT = columnTop-8;
if(bR[rowT]) {
return false;
}
bR[rowT] = true;
}
}
{
// up \:
int columnTop = column - row;
if(columnTop >= 0) {
if(bTL[columnTop]) {
return false;
}
bTL[columnTop] = true;
}
else { // hits side:
int rowT = -columnTop;
if(bL[rowT]) {
return false;
}
bL[rowT] = true;
}
}
}
return true;
}
int val(int const * const board, int * const permutation) {
int res = 0;
for(int i = 0; i < 8; ++i) {
res += board[i + 8*permutation[i]];
}
return res;
}
void run(const int i, int const * const board, int * const permutation, int &best, PermutationHandler &ph) {
if(i == 7) {
int lastQueen = ph.root()->next->val;
permutation[7] = lastQueen;
if(boardOK(permutation)) {
best = std::max(best, val(board, permutation));
}
return;
}
// try all combinations:
PermutationNode *node = ph.root();
while(node->next != NULL) {
PermutationNode *n = node->next;
// remove n from permutation:
node->next = n->next;
permutation[i] = n->val;
if(boardOK(permutation, i+1))
run(i+1, board, permutation, best, ph);
// re-insert in permutation and go to next:
node->next = n; // n->next is already set (never changes)
node = n;
}
}
int get(int *board) {
int res = 0;
int permutation[8];
PermutationHandler ph;
ph.reset(8);
run(0, board, permutation, res, ph);
return res;
}
int main() {
int N;
int board[64];
std::cin >> N;
for(int ignore = 0; ignore < N; ++ignore) {
for(int i = 0; i < 64; ++i) {
std::cin >> board[i];
}
int res = get(board);
printf("%5i\n", res);
}
}