Created
March 11, 2017 07:57
-
-
Save corehello/abb9803dba2b1915863019dc7c66b06a to your computer and use it in GitHub Desktop.
8 queen puzzle solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
You can define the size of Queens | |
*/ | |
#define QUEEN 8 | |
int row[QUEEN+1]; | |
int l[2*QUEEN]; | |
int r[2*QUEEN]; | |
int rt[QUEEN+1]; | |
int c=0; | |
void solve8queen(int level) | |
{ | |
if (level <= QUEEN) | |
{ | |
int i = 1; | |
while(i<=QUEEN) | |
{ | |
if(row[i]!=1&&l[level+i-1]!=1&&r[QUEEN+i-level]!=1) | |
{ | |
row[i] = l[level+i-1] = r[QUEEN+i-level] = 1; | |
rt[level] = i; | |
solve8queen(level+1); | |
row[i] = l[level+i-1] = r[QUEEN+i-level] = 0; | |
} | |
i++; | |
} | |
} | |
else | |
{ | |
c++; | |
int i; | |
for(i = 1; i<=QUEEN; i++) | |
{ | |
printf("\t%d", rt[i]); | |
} | |
printf("\n"); | |
} | |
} | |
int | |
main(int ac, char *av[]) | |
{ | |
solve8queen(1); | |
printf("totally: %d solutions\n", c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, we can use bit to represent the row, l, r, rt, but it is not straightforward. Bit operations are always cool techs.