-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEightQueens.cpp
80 lines (65 loc) · 1.61 KB
/
EightQueens.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
#include<iostream>
using namespace std ;
void eightqueen( char chess[][8] , int num );
void same( char dup[][8] , char chess[][8] ) ;
int count = 0 ;
int main()
{
char chess[8][8] ;
for( int i = 0 ; i < 8 ; i++)
{
for( int j = 0 ; j < 8 ; j++)
{
chess[i][j] = '.' ;
}
}
eightqueen( chess , 0 ) ;
return 0 ;
}
void same( char dup[][8] , char chess[][8] )
{
for( int i = 0 ; i < 8 ; i++)
{
for( int j = 0 ; j < 8 ; j++)
{
dup[i][j] = chess[i][j] ;
}
}
}
void eightqueen( char chess[][8] , int num )
{
char dup[8][8] ;
if( num == 8 ) { for( int i = 0 ; i < 8 ; i++)
{
for( int j = 0 ; j < 8 ; j++)
{
cout << chess[i][j] << " ";
}
cout << endl ;
}
cout << endl << endl ;
// cout << ++count << endl ;
return ;
}
for( int j = 0 ; j < 8 ; j++)
{
same( dup , chess ) ;
if( dup[num][j] == '.') dup[num][j] = 'Q';
else continue ;
for( int k = 0 ; k < 8 ; k++)
{
if( k != j ) dup[num][k] = 'o';
}
for( int k = 0 ; k < 8 ; k++)
{
if( k != num ) dup[k][j] = 'o';
}
for( int k = -7 ; k < 8 ; k++)
{
if( (num + k >= 0) && (num + k <= 7) && ( j + k >= 0 ) && ( j + k <= 7 ) && k != 0 ) dup[num + k][j + k] = 'o' ;
if( (num + k >= 0) && (num + k <= 7) && ( j - k >= 0 ) && ( j - k <= 7 ) && k != 0 ) dup[num + k][j - k] = 'o' ;
}
eightqueen( dup , num + 1 ) ;
}
return ;
}