-
Notifications
You must be signed in to change notification settings - Fork 0
/
750.cpp
127 lines (122 loc) · 2.39 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
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
//the most fucking output format i have ever seen
#include <bits/stdc++.h>
using namespace std;
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
#define FN(s,c) (int)s.find(c)
#define FOR(i,e) for (int i = 0; i < e; i++)
#define MFOR(i,s,e) for (int i = s; i <= e; i++)
#define ROF(i,s) for (int i = s-1; i >= 0; i--)
#define MROF(i,s,e) for (int i = s; i >= e; i--)
#define IT(it,v) for(it=v.begin();it!=v.end();it++)
#define TI(it,v) for(it=v.rbegin();it!=v.rend();it++)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FILL(a,v) memset(a,v,sizeof(a))
typedef long long ll;
/*************************************************/
vector<vector<pair<int, int> > > sol;
vector<pair<int, int> > queen;
bool valid(int r, int c) {
FOR(i,SZ(queen))
{
int tmp1, tmp2;
tmp1 = abs(queen[i].F - r);
tmp2 = abs(queen[i].S - c);
if (tmp1 == tmp2) {
return false;
}
}
return true;
}
bool vis[9] = { 0 };
void chess(int row) {
if (row > 8) {
sol.PB(queen);
return;
}
for (int c = 1; c <= 8; ++c) {
if (vis[c] == 1 || !valid(row, c)) {
continue;
}
vis[c] = 1;
queen.PB(MP(row, c));
chess(row + 1);
queen.pop_back();
vis[c] = 0;
}
}
bool comp(pair<int, int> first, pair<int, int> second) {
if (first.F < second.F) {
return 1;
}
return 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
chess(1);
int t;
cin >> t;
string s;
FOR(i,t)
{
int a, b;
getline(cin, s);
cin >> a >> b;
vector<vector<pair<int, int> > > ans;
pair<int, int> ptmp = MP(a, b);
FOR(j,SZ(sol))
{
bool check = 0;
vector<pair<int, int> > vtmp;
FOR(k,SZ(sol[j]))
{
if (sol[j][k] == ptmp) {
check = 1;
}
vtmp.PB(MP(sol[j][k].S, sol[j][k].F));
}
if (check) {
ans.PB(vtmp);
}
}
vector<vector<int> > fans;
FOR(j,SZ(ans))
{
sort(ALL(ans[j]), comp);
vector<int> vtmp;
FOR(k,SZ(ans[j]))
{
vtmp.PB(ans[j][k].S);
}
fans.PB(vtmp);
}
sort(ALL(fans));
cout << "SOLN COLUMN" << endl;
cout << " # 1 2 3 4 5 6 7 8" << endl << endl;
FOR(j,SZ(fans))
{
printf("%2d ", j + 1);
FOR(k,SZ(fans[j]))
{
cout << fans[j][k];
if (k != SZ(fans[j]) - 1) {
cout << ' ';
}
}
// if (j != SZ(fans) - 1) {
cout << endl;
// }
}
if (i != t - 1) {
cout << endl;
}
}
return 0;
}