-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8queensOJ.cpp
118 lines (102 loc) · 2.3 KB
/
8queensOJ.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int Y, X, count2;
bool g(int y, int x)
{
return y >= 0 && y <= 7 && x >= 0 && x <= 7;
}
void cross(vector<vector<bool>> & a, int Y, int X)
{
int tempY, tempX;
tempY = Y;
tempX = X;
while (g(tempY, tempX))
{
a[tempY][tempX] = true;
--tempY;
++tempX;
}
tempY = Y;
tempX = X;
while (g(tempY, tempX))
{
a[tempY][tempX] = true;
++tempY;
++tempX;
}
a[Y][X] = false;
}
void solve(vector<vector<bool>> & a, vector<bool> & rows, int nowColumn, vector<int> ans)
{
if (nowColumn == X)
{
vector<int> temp1 = ans;
temp1.push_back(Y);
if (a[Y][X] == false)
{
solve(a, rows, nowColumn + 1, temp1);
}
}
else if (nowColumn == 8)
{
if (count2 <= 9)
{
cout << ' ';
}
cout << count2 << " ";
for (int i = 0; i <= 7; ++i)
{
cout << ' ' << ans[i] + 1;
}
cout << '\n';
++count2;
}
else
{
for (int y = 0; y <= 7; ++y)
{
if (rows[y] == false && a[y][nowColumn] == false)
{
vector<vector<bool>> temp1 = a;
vector<bool> temp2 = rows;
vector<int> temp3 = ans;
cross(temp1, y, nowColumn);
temp2[y] = true;
temp3.push_back(y);
solve(temp1, temp2, nowColumn + 1, temp3);
}
}
}
}
int main()
{
int count1;
cin >> count1;
for (int i = 1; i <= count1; ++i)
{
count2 = 1;
vector<vector<bool>> a(8, vector<bool>(8, false));
vector<bool> rows(8, false);
vector<int> ans;
cin >> Y >> X;
--Y; --X;
rows[Y] = true;
cross(a, Y, X);
cout << "SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n";
solve(a, rows, 0, ans);
if (i != count1)
{
cout << '\n';
}
}
return 0;
}