-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8queens1DwGOTO.cpp
More file actions
56 lines (46 loc) · 847 Bytes
/
Copy path8queens1DwGOTO.cpp
File metadata and controls
56 lines (46 loc) · 847 Bytes
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
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int q[8] = {0};
int r = 0, c = 0;
q[0] = 0;
nextCol:
//next colum
c++;
if (c==8){
goto print;
}
q[c] = -1;
nextRow:
//next row
q[c]++;
if (q[c]==8){
goto backtrack;
}
//row test
for(int i = 0; i < c; i++)
if((q[i] == q[c]) || (abs(q[c] - q[i])== c-1))) goto nextRow;
goto nextCol;
backtrack:
c--;
if(c == -1) return 0;
goto nextRow;
print:
static int numSolutions = 0;
cout << "Solution #" << ++numSolutions<< ":\n";
//print out queens
for(int i=0;i<8;i++){
cout<<endl;
for(int j=0; j<8;j++){
if(q[i]==j){
cout<<"1";
}
else{
cout<<"0";
}
}
}
cout<<endl;
goto backtrack;
}