-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8QUEENHS.C
227 lines (181 loc) · 4.15 KB
/
8QUEENHS.C
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
//Following are ASCII values of Box drawing characters
#define LT 218 // ┌
#define RT 191 // ┐
#define LB 192 // └
#define RB 217 // ┘
#define HL 196 // ─
#define VL 179 // │
#define PLUS 197 // ┼
#define TT 194 // ┬
#define BT 193 // ┴
#define LST 195 // ├
#define RST 180 // ┤
#define DIM 8 // 8 x 8
#define WIDTH 7
#define HEIGHT 1
#define SAFE 1
#define NOTSAFE 0
void delay(int);
void gotoxy(short, short);
void drawBox( int , int);
void placeQueen( int , int);
void removeQueen( int , int);
int isSafe(int , int) ;
int queenpos[8]={ -1,-1,-1, -1, -1, -1, -1, -1};
int sol[92][8];
int main()
{
int row , col ,ctr , safecol = 0 , count = 0;
row = ( 25 - ( DIM * HEIGHT + DIM + 1)) / 2;
col = ( 80 - (DIM * WIDTH + DIM + 1)) / 2;
drawBox( row , col);
for(row = 0 ; row < 8 ; row++)
{
for(col = safecol ; col < 8 ; col++)
{
if ( isSafe( row , col) == SAFE )
{
queenpos[row] = col;
placeQueen( row , col);
safecol = 0;
break;
}
} // for col
// backtrack
if(col == 8)
{
safecol = queenpos[row -1 ];
removeQueen( row-1 , safecol);
queenpos[ row -1] = -1;
safecol++;
row = row - 2;
}
if( row == 7)
{
for( ctr = 0 ; ctr < 8 ; ctr++)
sol[count][ctr] = queenpos[ctr];
gotoxy(1 , 1);
printf("SOL#%d" , ++count);
delay(1);
if( count == 92)
break;
safecol = queenpos[row];
removeQueen( row , safecol);
queenpos[row] = -1;
row--;
safecol++;
}
}// for row
return 0;
}
//Moves Cursor to Position Specified i.e at (x,y)
void gotoxy(short col, short row)
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD position={col,row};
SetConsoleCursorPosition(h,position);
}
void delay(int number_of_seconds)
{
// Converting time into milliseconds
int milli_seconds = 1000 * number_of_seconds;
// Storing start time
clock_t start_time = clock();
// looping till required time is not achieved
while (clock() < start_time + milli_seconds)
;
}
int isSafe(int r , int c)
{
int sr = r , sc= c;
// up
for( r ; r != -1; r--)
{
if( queenpos[r] == c)
return NOTSAFE;
}
// left diagonal
for( r = sr ; r != -1 && c != -1 ;r-- , c--)
{
if( queenpos[r] == c)
return NOTSAFE;
}
// right diagonal
for( r= sr , c = sc ; r != -1 && c != 8 ; c++ , r--)
{
if( queenpos[r] == c)
return NOTSAFE;
}
return SAFE;
}
void placeQueen( int r , int c)
{
int sc_row , sc_col , brow , bcol;
sc_row = ( 25 - ( DIM * HEIGHT + DIM + 1)) / 2;
sc_col = ( 80 - (DIM * WIDTH + DIM + 1)) / 2;
brow = sc_row + 1;
bcol = sc_col + 2;
gotoxy( bcol + 8 * c , brow + 2 * r);
printf("Queen");
}
void removeQueen( int r , int c)
{
int sc_row , sc_col , brow , bcol;
sc_row = ( 25 - ( DIM * HEIGHT + DIM + 1)) / 2;
sc_col = ( 80 - (DIM * WIDTH + DIM + 1)) / 2;
brow = sc_row + 1;
bcol = sc_col + 2;
gotoxy( bcol + 8 * c , brow + 2 * r);
printf("%c%c%c%c%c", 32, 32, 32, 32, 32);
}
void drawBox( int r , int c)
{
int ctr , ctr1 , ctr2;
// first line
gotoxy( c , r);
printf("%c" , LT);
for(ctr1 = 0 ; ctr1 < DIM ; ctr1++)
{
for( ctr = 0 ; ctr < WIDTH ; ctr++)
printf("%c" , HL);
printf("%c" , TT);
}
printf("\b%c" , RT);
// second line
for(ctr2 = 0 ; ctr2 < DIM ; ctr2++)
{
r++;
gotoxy( c , r) ;
for(ctr1 = 0 ; ctr1 < DIM+1 ; ctr1++)
{
printf("%c", VL);
for( ctr = 0 ; ctr < WIDTH ; ctr++)
printf("%c" , 32);
}
// third line
r++;
gotoxy( c, r);
printf("%c" , LST);
for(ctr1 = 0 ; ctr1 < DIM ; ctr1++)
{
for(ctr = 0 ; ctr < WIDTH ; ctr++)
printf("%c" , HL);
printf("%c", PLUS);
}
printf("\b%c", RST);
}
// last line
gotoxy( c , r);
printf("%c" , LB);
for(ctr1 = 0 ; ctr1 < DIM ; ctr1++)
{
for( ctr = 0 ; ctr < WIDTH ; ctr++)
printf("%c" , HL);
printf("%c" , BT);
}
printf("\b%c" , RB);
}