-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8queens2D.cpp
More file actions
63 lines (56 loc) · 1.56 KB
/
Copy path8queens2D.cpp
File metadata and controls
63 lines (56 loc) · 1.56 KB
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
/********************************
* Name: Julian Moskovits
* Date: 9/13/2016
* Description: Prints out every possible way to place 8 queens on a chess board without any queen being able to kill another.
* Uses a 2D Array to represent a chessboard.
********************************/
#include <iostream>
using namespace std;
int main()
{
int count = 0;
int b[8][8] = {0}, c = 0, r = 0;
b[0][0] = 1;
//Go to the next column, print if you're past the last column and go through the rows in that column if not.
NC:
c++;
if (c == 8){
goto print;
}
r = -1;
//In whichever column you're in, go to the next row to see if it can hold a queen. If you've gone through all rows and couldn't, backtrack.
NR:
r++;
if (r == 8){
goto backtrack;
}
for (int i = 0; i < c; ++i) if (b[r][i] == 1) goto NR;
for (int i = 0; i < r && i < c; ++i) if (b[r - i - 1][c - i - 1] == 1) goto NR;
for (int i = 0; i < c && i < (7 - r); ++i) if (b[r + i + 1][c - i - 1] == 1) goto NR;
b[r][c] = 1;
goto NC;
//Go to the previous column and put the queen somewhere else. The current position won't work.
backtrack:
c--;
//If you've backtracked past the first column, you've already gone through all possibilities. End the program.
if (c == -1){
return -1;
}
r = 0;
while (b[r][c] != 1){
r++;
}
b[r][c] = 0;
goto NR;
print:
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j){
cout << b[i][j] << " ";
}
cout << endl << endl;
}
cout << ++count << endl << endl << endl;
//Continue to find more solutions.
goto backtrack;
return 0;
}