-
Notifications
You must be signed in to change notification settings - Fork 3
/
8queen.cpp
117 lines (110 loc) · 2.78 KB
/
8queen.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
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
//code to solve 8 queen problem(testing)
int aray[8][8];
char aray1[8][8];
int i,j,k,l,m,n,p,q;
bool flag;
for(i=0;i<8;i++){
for(j=0;j<8;j++){
aray[i][j]=0;
aray1[i][j]='x';
}
}
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(j!=7){
printf("%d ",aray[i][j]);
}else{
printf("%d",aray[i][j]);
}
}
printf("\n");
}
printf("\n");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(j!=7){
printf("%c ",aray1[i][j]);
}else{
printf("%c",aray1[i][j]);
}
}
printf("\n");
}
printf("\n");
i=0;
j=0;
do{
flag=true;
while(flag==true){
if(aray[i][j]==0){
flag=false;
}else{
if(j<8){
j++;
}else{
if(i<8){
i++;
}
}
}
}
printf(" checked for 0 found ");
if(aray[i][j]==0){
aray[i][j]=1;
aray1[i][j]='Q';
for(k=0;k<8;k++){
aray[i][k]=1;
}
for(l=0;l<8;l++){
aray[l][j]=1;
}
for(m=i+1,n=j+1;m<8 && n<8;m++,n++){
aray[m][n]=1;
}
for(m=i+1,n=j-1;m<8 && n>0;m++,n--){
aray[m][n]=1;
}
for(m=i-1,n=j+1;m>0 && n<8;m--,n++){
aray[m][n]=1;
}
for(m=i-1,n=j-1;m>0 && n>0;m--,n--){
aray[m][n]=1;
}
}
if(i<8){
i++;
}else{
i=0;
if(j<8){
j++;
}
}
}while(!flag);
printf("\n");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(j!=7){
printf("%d ",aray[i][j]);
}else{
printf("%d",aray[i][j]);
}
}
printf("\n");
}
printf("\n");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(j!=7){
printf("%c ",aray1[i][j]);
}else{
printf("%c",aray1[i][j]);
}
}
printf("\n");
}
}