-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8queens_bruteforce.cpp
More file actions
82 lines (62 loc) · 1.82 KB
/
Copy path8queens_bruteforce.cpp
File metadata and controls
82 lines (62 loc) · 1.82 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//CS 211 Assignment #7
//Queens Algorithm brute force method
//Difficulty: 8
#include<cmath>
#include<iostream>
using namespace std;
bool ok(int board[][8]){ //validity function
for(int c=7; c >= 0; c--) {
int r=0;
while(board[r][c] != 1 ) {
r++;
}
for (int i = 0; i<c; i++) //row check
if (board[r][i]==1) return false;
for (int k = 1;(r-k) >=0 && (c-k)>=0; k++) //up diag check
if (board[r-k] [c-k] == 1) return false;
for (int z = 1; (r+z) <=7 && (c-z) >=0; z++) //down diag check
if (board[r+z] [c-z] == 1) return false;
}
return true; //all checks passed return true
}
void print(int board[][8], int count) {
cout <<"Solution "<< count <<":"<< endl;
for(int r = 0; r < 8; r++) {
for(int c = 0; c < 8; c++) {
cout << board[r][c]<<" ";
}
cout << endl;
}
cout << endl;
}
int main( ) {
int board[8][8]={0};
int count = 0;
for(int i0 =0; i0 <8; i0 ++)
for(int i1 =0; i1 <8; i1 ++)
for(int i2 =0; i2 <8; i2 ++)
for(int i3 =0; i3 <8; i3 ++)
for(int i4 =0; i4 <8; i4 ++)
for(int i5 =0; i5 <8; i5 ++)
for(int i6 =0; i6 <8; i6 ++)
for(int i7 =0; i7 <8; i7 ++){ //setup board for checking
board[i0][0]=1;
board[i1][1]=1;
board[i2][2]=1;
board[i3][3]=1;
board[i4][4]=1;
board[i5][5]=1;
board[i6][6]=1;
board[i7][7]=1;
if(ok(board)) print(board, ++count);
board[i0][0]=0; //wipe board after print and check
board[i1][1]=0;
board[i2][2]=0;
board[i3][3]=0;
board[i4][4]=0;
board[i5][5]=0;
board[i6][6]=0;
board[i7][7]=0;
}
return 0;
}