-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_Array_8Queens_Goto.cpp
55 lines (48 loc) · 1.53 KB
/
2D_Array_8Queens_Goto.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
/*Objective: Find all Possible placements of Queens on an 8x8 board
such that no Queens are killed by another queens with 2D Array
*/
#include <iostream>
using namespace std;
int main(){
int board[8][8] = {}, row,col = 0, i;
board[0][0] = 1;
int solution = 0;
nextCol: col++;
if( col==8) goto print;
row = -1;
nextRow: row++;
if(row == 8)
goto backtrack;
//row test
for(i = 0; i < col; i++)
if(board[row][i] == 1) goto nextRow;
//up diagonal test
for(i = 1; (row-i)>=0 && (col-i) >= 0; i++){
if(board[row-i][col-i] == 1)
goto nextRow;
}
//down diagonal test
for(i = 1; (row+i)<8 && (col -i) >= 0; i++){
if(board[row+i][col-i] ==1)
goto nextRow;
}
board[row][col]=1;
goto nextCol;
backtrack: col--;
if(col== -1) return 0;
row = 0;
while(board[row][col]!=1)
row++;
board[row][col]=0;
goto nextRow;
print:
cout <<"Solution " << ++solution << endl;
for(int row = 0; row < 8; row++){
for(int col = 0; col < 8; col++){
cout << board[row][col];
}
cout << endl;
}
cout << endl;
goto backtrack;
}