Guys I am trying to write a code for solving the 8 queen problem But the Problem is I cannot Print out the Result. There is something really weird going on. the function showsol is not doing its job. It is messed up for some reason. Can anyone tell me why the 2d Array is not being printed by the "Showsol" Function?. It leaves the whole screen blank.
Code:
#include <stdio.h>
#include <conio.h>
int cb[8][8]={{0},{0},{0},{0},{0},{0},{0},{0}};
int test(r,c)
{
if(cb[r][c]!=0)
{
return 0;
}
else
return 1;
}
void mark(int r,int c)
{
int a;
int x=r,y=c;
for(a=0;a<8;a++)
{
cb[r][a]=1;
}
for(a=0;a<8;a++)
{
cb[a][c]=1;
}
while(x>0&&y>0)
{
cb[--x][--y]=1;
}
x=r;
y=c;
while(x<8&&y<8)
{
cb[++x][++y]=1;
}
cb[r][c]=2;
}
void unmark(int r,int c)
{
int a;
for(a=0;a<8;a++)
{
cb[r][a]=0;
}
for(a=0;a<8;a++)
{
cb[a][c]=0;
}
while(r>=0&&c>=0)
{
cb[--r][--c]=0;
}
while(r<8&&c<8)
{
cb[++r][++c]=0;
}
cb[r][c]=0;
}
void showsol()
{
int count1,count2;
printf("The Result is\n");
for(count1=0;count1<8;count1++)
{
for(count2=0;count2<8;count2++)
{
printf(" %d ",cb[count1][count2]);
}
printf("\n");
}
}
void queen8(int n)
{
int i;
for(i=0;i<8;i++)
{
if(test(n,i)==1)
{
mark(n,i);
if(n<7)
queen8(n+1);
else if(n==7)
{
showsol();
getch();
exit(0);
}
}
}
unmark(n,i);
}
int main()
{
clrscr();
getch();
queen8(0);
getch();
return 0;
}