Home > Back-end >  N queen problem
N queen problem

Time:09-18

A great god, please help to see what is the problem, thank you
#include
#include
# define Max 4
Int queen [Max + 1)={0};
int count=0;
Int IsValid (int m);
Void the print ();
Void place (int m);
Void main ()
{
Int m=1;
Place (m);
Printf (" % d ", count);

}
Void place (int m)
{
for(int i=1; I<=Max; I++)
{
Queen [m] + +;
If (queen [m] <=Max)
{
If (IsValid (m))
{
If (m==Max)
{
Print ();
count++;
}
The else
Place (m + 1);
}
}
The else
{
Queen [m]=queen [m] - Max;
}
}
Queen [m]=0;
Place (m - 1);

}
Int IsValid (int m)
{
int i;
for(i=1; IIf (queen [m]==queen [I] | | (m - I)==abs (queen [m] - queen [I]))
Return 0;
return 1;

}
Void the print ()
{
int i,j;
for(i=1; I<=Max; I++)
{
for(j=1; J<=Max; J++)
If (queen [I]==j)
Printf (" 1 ");
The else
Printf (" 0 ");
printf("\n");
}
printf("\n");
}

CodePudding user response:

Choose that, can't choose, you didn't do
  • Related