程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> 求從棋盤的坐下角到右上角的無環路的總數

求從棋盤的坐下角到右上角的無環路的總數

編輯:C語言基礎知識
#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:"<<num;
    getchar();
    }//main
  
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved