龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

求从棋盘的坐下角到右上角的无环路的总数

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
#include"stdio.h" #define N 8 //因为算出来的数据太大,所以要算很久,可以改变N的值进行测试。 #include"iostream.h" //此算法采用回溯法 enum bin{fal,tr}; //假如有更好的算法,请发e-mail给我。在此

#include"stdio.h"

  #define N 8 //因为算出来的数据太大,所以要算很久,可以改变N的值进行测试。

  #include"iostream.h" //此算法采用回溯法

enum bin{fal,tr}; //假如有更好的算法,请发e-mail给我。在此谢过。

  int top=0;

  long int num=0;

  int row[]={-1,-2,-2,-1,1,2,2,1};

  int col[]={-2,-1,1,2,2,1,-1,-2};

  bin mark[N][N];

strUCt stack

  {

   int x,y;

   int dir;}board[N*N];

void push(stack it)

  {

   board[top].x=it.x;

   board[top].y=it.y;

   board[top].dir=it.dir;

   mark[board[top].x][board[top].y]=tr;

   top++;

   }

  

  stack pop()

  {

   --top;

   mark[board[top].x][board[top].y]=fal;

   board[top].dir++;

   return board[top];

   }

  

  bin empty()

  {

   if(top==0) return tr;

   else return fal;

   }

  

  void main()

  {

   stack temp={N-1,N-1,-1};

   push(temp);

   while(!empty())

   {

   int g,h;

   temp=pop();

   int i=temp.x;

   int j=temp.y;

   int dir=temp.dir;

   while(dir<8)

   {

   g=i+row[dir];

   h=j+col[dir];

   if((g<0)(h<0)(g>=N)(h>=N)mark[g][h]) dir++;

   else {

   if(g==0&&h==0) {num++;dir++;}

   else {

   temp.x=i;

   temp.y=j;

   temp.dir=dir;

   push(temp);

   i=g;

   j=h;

   dir=0;

   }//else

   }//else

   }//while

   }//while

   cout<<"the total number is:"<

   getchar();

   }//main

  

  

精彩图集

赞助商链接