算法之8皇后问题(C语言)

     老大的老大说算法很重要,于是就有了......
     有太长太长的时间没有写过代码了,手都生了!
#include "stdio.h"
#define MAX_X 8
#define MAX_Y 8
#define OBS   (MAX_X-1)
int P[MAX_X][MAX_Y];//Queen's point,true == has Queen,false == not has
int L[MAX_X*2-1];//MAX_X*2-1 is the number of Diagonal-L; L is the states of left Diagonal,
              //true == this left Diagonal not locked, false == this left Diagonal is locked
int R[MAX_Y*2-1];//MAX_Y*2-1 is the number of Diagonal-R; R is the states of right Diagonal,
            //true == this right Diagonal not locked, false == this right Diagonal is locked
int C[MAX_Y];//states of colums
             //false == this colum is locked; true == this colum is not locked
int nNumberOfFunctions = 0;
void PutNextOne(int nLine);
  
void PrintPoint()
{
 int i = 0;
 int j = 0;
 printf("Function %d is:\r\n",nNumberOfFunctions);
 
 for(i = 0; i < MAX_X; i++)
 {
  for(j = 0; j < MAX_Y; j++)
  {
   if(P[i][j] == 1)
   {
    printf("x = %d, y = %d\r\n",i,j);
   }
  }
 }
 printf("\r\n");
}
void PutNextOne(int nLine)
{
 int i = 0;
 if(nLine >= MAX_X)
 {
  nNumberOfFunctions++;
  PrintPoint();
  return ;
 }
 for(i = 0; i < MAX_Y; i++)
 {
  if((C[i] == 1) && (L[nLine+i] == 1) && (R[nLine-i+OBS] == 1))
  {
   C[i] = 0;
   L[nLine+i] = 0;
   R[nLine-i+OBS] = 0;
   P[nLine][i] = 1;
   
   PutNextOne(nLine+1);
   C[i]= 1;
   L[nLine+i] = 1;
   R[nLine-i+OBS] = 1;
   P[nLine][i] = 0;
  }
 }
 return;
}

void main()
{
 int i = 0;
 int j = 0;
 for(i = 0; i < MAX_Y; i++)
 {
  C[i] = 1;
 }
 for(i = 0; i < MAX_X*2-1; i++)
 {
  L[i] = 1;
 }
 for(i = 0; i < MAX_Y*2-1; i++)
 {
  R[i] = 1;
 }
 for(i = 0; i < MAX_X; i++)
 {
  for(j = 0; j < MAX_Y; j++)
  {
   P[i][j] = 0;
  }
 }
 PutNextOne(0);
}
   

你可能感兴趣的:(算法,问题,C语言,休闲,8皇后)