-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnQueen.c
83 lines (67 loc) · 1.31 KB
/
nQueen.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
#include<stdio.h>
#include<stdlib.h>
#define SAFE 1
#define NOTSAFE 0
int isSafe(int , int) ;
int queenpos[8]={ -1,-1,-1, -1,-1,-1,-1, -1};
int sol[][8];
int main()
{
int DIM,row , col ,ctr , safecol = 0 , count = 0,i;
scanf("%d",&DIM);// DIM x DIM
for(row = 0 ; row < DIM ; row++)
{
for(col = safecol ; col < DIM ; col++)
{
if ( isSafe( row , col) == SAFE )
{
queenpos[row] = col;
safecol = 0;
break;
}
} // for col
// backtrack
if(col == DIM)
{
safecol = queenpos[row -1 ];
queenpos[ row -1] = -1;
safecol++;
row = row - 2;
}
if( row == DIM-1)
{
for(i=0;i<DIM;i++){
printf("%d ",queenpos[i]+1);
}
printf("\n");
safecol = queenpos[row];
queenpos[row] = -1;
row--;
safecol++;
}
}// for row
return 0;
}
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;
}