knight遍历棋盘问题,回溯应用

来源:互联网 发布:update例子 mysql 编辑:程序博客网 时间:2024/05/29 18:37

能给出所有符合标准的答案...

很早前写的了,还是个C风格的,照着书上的迷宫问题写法改的,可能是我至今写过的最规范的程序....

数据的输入,三个数字,逗号隔开,第一个n表示棋盘为n*n的,接下来两个数字表示起点的坐标(1<= x,y<=n),比如 5,3,3

注意:程序没有考虑错误输入的情况..也没有考虑答案过多或者没有答案的相关处理方法,通常答案比较多,所以增加了同目录下的文件写入,方便查看各种组合...

很早的程序,还是VC6下调的..

 

 

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 20
  4. typedef struct
  5. {
  6.     int x,y;
  7. }CellType;
  8. typedef struct
  9. {
  10.     CellType path[N*N];
  11.     int length;
  12. }KnightPathType;
  13. void outSolution(short int knight[][N],int n);
  14. void trySolution(short int knight[][N],int n,CellType cur,KnightPathType knightPath);
  15. void knightSolution(short int knight[][N],int n,CellType first);
  16. int main(void)
  17. {
  18.     short int knight[N][N]={0};
  19.     //printf("%d   %d   %d/n",knight[12][3],knight[2][4],knight[5][2]);//调试代码,看是否全赋上初值0;
  20.     int n;
  21.     CellType first;
  22.     scanf("%d,%d,%d", &n ,&first.x ,&first.y );
  23.     first.x--;
  24.     first.y--;
  25.     knightSolution(knight,n,first);
  26.     printf("执行结束");
  27.     system("PAUSE");
  28.     return 0;
  29. }
  30. void knightSolution(short int knight[][N],int n,CellType first)
  31. {
  32.     KnightPathType knightPath;
  33.     knightPath.length=0;
  34.     knightPath.path[knightPath.length].x=first.x;
  35.     knightPath.path[knightPath.length].y=first.y;
  36.     knight[first.y][first.x]=1;
  37.     knightPath.length++;
  38.     trySolution(knight,n,first,knightPath);
  39. }
  40. void trySolution(short int knight[][N],int n,CellType cur,KnightPathType knightPath)
  41. {
  42.     int i;
  43.     int xShift[8]={-2,-2,-1,-1,1,1,2,2};
  44.     int yShift[8]={-1,1,-2,2,-2,2,-1,1};
  45.     CellType adjCell;
  46.     if(knightPath.length==n*n)
  47.     {
  48.         outSolution(knight,n);
  49.     }
  50.     else
  51.     {
  52.         for(i=0;i<8;i++)
  53.         {
  54.             adjCell.x=cur.x+xShift[i];
  55.             adjCell.y=cur.y+yShift[i];
  56.             if(adjCell.x>=0 && adjCell.x<n && adjCell.y>=0 && adjCell.y<n && (knight[adjCell.y][adjCell.x]==0) )
  57.             {
  58.                 knightPath.path[knightPath.length].x =adjCell.x;
  59.                 knightPath.path[knightPath.length].y =adjCell.y;
  60.                 knightPath.length++;
  61.                 knight[adjCell.y][adjCell.x]=knightPath.length;
  62.                 trySolution(knight,n,adjCell,knightPath);
  63.                 knightPath.length--;
  64.                 knight[adjCell.y][adjCell.x]=0;
  65.             }
  66.         }
  67.     }
  68. }
  69. void outSolution(short int knight[][N],int n)
  70. {
  71.     static int num=0;
  72.     int i,j;
  73.     FILE *pFile;
  74.     if( (pFile=fopen("result.txt","at+"))==NULL )
  75.     {
  76.         printf("打开文件失败/n任意键退出。。。/n");
  77.         getchar();
  78.         exit(1);
  79.     }
  80.     fprintf(pFile,"第%d条路径:/n",++num);
  81.     printf("第%d条路径:/n",num);
  82.     for(i=0;i<n;i++)
  83.     {
  84.         for(j=0;j<n;j++)
  85.         {
  86.             printf("%3d",knight[i][j]);
  87.             fprintf(pFile,"%3d",knight[i][j]);
  88.         }
  89.         printf("/n");
  90.         fprintf(pFile,"/n");
  91.     }
  92.     fclose(pFile);
  93. }
原创粉丝点击