EightQueens in the game of chess

国际象棋八皇后相容排阵问题:

My code of today

#include <iostream.h>
//八皇后求解
int k=0,result[10]={0};
int main()
{
 void search();
 void shresult();
 search();
 
 return(0);
}

//Show the result
void shresult()
{
 for(k=0;k<8;k++) cout<<result[k]<<',';
 cout<<"/t OK"<<endl;
}

//Searching the answer for the problem.
void search()
{
 int i,j,judge;
 for(i=0;i<8;i++)
 {
  judge=1;
  for(j=0;j<k;j++)
  {
   if(i==result[j])  judge&=0;
   if(i==(result[j]-k+j)) judge&=0;
   if(i==(result[j]+k-j)) judge&=0;
  }
  if(judge){result[k]=i;k++;search();}
 }
 if(!(k<8)) shresult();
 k--;
}

你可能感兴趣的:(game)