-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path750.cpp
50 lines (45 loc) · 1.3 KB
/
750.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
/*
brute force > iterative > all permutations
difficulty: easy
date: 12/Jan/2020
by: @brpapa
*/
#include <cstdio>
#include <vector>
#include <algorithm>
#define abs(a) ((a) > 0 ? (a) : -(a))
using namespace std;
struct Taux {
int row[8]; // row[c] = linha da coluna c
};
vector<Taux> validArrangements;
int main() {
// pré-processa todas as disposições válidas (q nao compartilham diagonais)
int row[8] = {0, 1, 2, 3, 4, 5, 6, 7};
do {
bool valid = true;
for (int i = 0; i < 8 && valid; i++)
for (int j = i+1; j < 8 && valid; j++)
if (abs(i-j) == abs(row[i]-row[j]))
valid = false;
if (valid) {
Taux tmp; for (int i = 0; i < 8; i++) tmp.row[i] = row[i];
validArrangements.push_back(tmp);
}
} while (next_permutation(row, row+8));
int T; scanf("%d", &T);
while (T--) {
int r0, c0; scanf("%d %d", &r0, &c0); r0--; c0--;
int s = 1;
printf("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n");
for (Taux va : validArrangements)
if (va.row[c0] == r0) {
printf("%2d ", s++);
for (int c = 0; c < 8; c++)
printf(" %d", va.row[c]+1);
printf("\n");
}
printf("%s", T > 0? "\n" : "");
}
return 0;
}