国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

求從棋盤的坐下角到右上角的無環(huán)路的總數(shù)

2019-11-17 05:33:12
字體:
供稿:網(wǎng)友
#include"stdio.h"
#define N 8          //因?yàn)樗愠鰜淼臄?shù)據(jù)太大,所以要算很久,可以改變N的值進(jìn)行測(cè)試。
#include"iostream.h" //此算法采用回溯法 enum bin{fal,tr};    //假如有更好的算法,請(qǐng)發(fā)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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 社旗县| 海城市| 辰溪县| 武夷山市| 合山市| 海门市| 石狮市| 桦甸市| 武鸣县| 墨竹工卡县| 南康市| 嘉祥县| 凤凰县| 固始县| 黔南| 高陵县| 张家口市| 朔州市| 文昌市| 乌海市| 阆中市| 大洼县| 仁化县| 定结县| 客服| 天台县| 雷州市| 山东省| 崇州市| 姚安县| 大关县| 色达县| 新宁县| 黎川县| 南皮县| 独山县| 班玛县| 承德县| 旬邑县| 股票| 富裕县|