鱼C论坛

 找回密码
 立即注册
查看: 2154|回复: 2

马踏棋盘第127行的if语句是什么作用

[复制链接]
发表于 2018-5-14 21:11:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <time.h>
  3. #define X 8
  4. #define Y 8
  5. int chess[X][Y];
  6. // 判断下一步可不可走,可走则走,不可走则返回0
  7. int nextxy(int *x, int *y, int count) //count 0~7表示八个方位
  8. {
  9.         switch(count)
  10.         {
  11.                 case 0:
  12.                                         if( *x+2<=X-1 && *y-1>=0 && chess[*x+2][*y-1]==0 )
  13.                 {
  14.                         *x = *x + 2;
  15.                         *y = *y - 1;
  16.                         return 1;
  17.                 }
  18.                 break;
  19.                 case 1:
  20.                                         if( *x+2<=X-1 && *y+1<=Y-1 && chess[*x+2][*y+1]==0 )
  21.                 {
  22.                         *x = *x + 2;
  23.                         *y = *y + 1;
  24.                         return 1;
  25.                 }
  26.                 break;
  27.                 case 2:
  28.                                         if( *x+1<=X-1 && *y-2>=0 && chess[*x+1][*y-2]==0 )
  29.                 {
  30.                         *x = *x + 1;
  31.                         *y = *y - 2;
  32.                         return 1;
  33.                 }
  34.                 break;
  35.                 case 3:
  36.                                         if( *x+1<=X-1 && *y+2<=Y-1 && chess[*x+1][*y+2]==0 )
  37.                 {
  38.                         *x = *x + 1;
  39.                         *y = *y + 2;
  40.                         return 1;
  41.                 }
  42.                 break;
  43.                 case 4:
  44.                                         if( *x-2>=0 && *y-1>=0 && chess[*x-2][*y-1]==0 )
  45.                 {
  46.                         *x = *x - 2;
  47.                         *y = *y - 1;
  48.                         return 1;
  49.                 }
  50.                 break;
  51.                 case 5:
  52.                                         if( *x-2>=0 && *y+1<=Y-1 && chess[*x-2][*y+1]==0 )
  53.                 {
  54.                         *x = *x - 2;
  55.                         *y = *y + 1;
  56.                         return 1;
  57.                 }
  58.                 break;
  59.                 case 6:
  60.                                         if( *x-1>=0 && *y-2>=0 && chess[*x-1][*y-2]==0 )
  61.                 {
  62.                         *x = *x - 1;
  63.                         *y = *y - 2;
  64.                         return 1;
  65.                 }
  66.                 break;
  67.                 case 7:
  68.                                         if( *x-1>=0 && *y+2<=Y-1 && chess[*x-1][*y+2]==0 )
  69.                 {
  70.                         *x = *x - 1;
  71.                         *y = *y + 2;
  72.                         return 1;
  73.                 }
  74.                 break;
  75.                 default:
  76.                                         break;
  77.         }
  78.         return 0;
  79. }
  80. void print()
  81. {
  82.         int i, j;
  83.         for ( i=0; i < X; i++ )
  84.         {
  85.                 for ( j=0; j < Y; j++ )
  86.                 {
  87.                         printf("%2d\t", chess[i][j]);
  88.                 }
  89.                 printf("\n");
  90.         }
  91.         printf("\n");
  92. }
  93. //走过一步,tag就加1
  94. int TravelChessBoard(int x, int y, int tag)
  95. {
  96.         int x1=x, y1=y, flag=0, count=0;   //x1,y1表示一下个位置,x,y表示当前位置,我们这里用了递归,所以x,y是变化的
  97.         chess[x][y] = tag;
  98.         // 如果全部位置走遍,则打印棋盘
  99.         if( tag == X*Y )
  100.         {
  101.                 print();
  102.                 return 1;  //返回给主函数
  103.         }
  104.         flag = nextxy(&x1, &y1, count);
  105.         while( 0==flag && count < 7 ) //如果初始位置在右上角(只是假设),count=0表示初始位置的右上角的位置,则case 0不可走,直接到了defult,跳出返回0,即flag等于0,然后看初始位置的其他方位是否可行
  106.         {
  107.                 count++;
  108.                 flag = nextxy(&x1, &y1, count);
  109.         }
  110.         while( flag ) //flag为1,表示可走为0表示不可走
  111.         {
  112.                 if( TravelChessBoard(x1, y1, tag+1) )
  113.                 {
  114.                         return 1;
  115.                 }
  116.                 x1 = x;     //如果走到左下角而且它的右上角的两个方位都走过,则回溯选择当前位置的其他方位
  117.                 y1 = y;
  118.                 count++;
  119.                 flag = nextxy(&x1, &y1, count);
  120.                 while( 0==flag && count < 7 )
  121.                 {
  122.                         count++;
  123.                         flag = nextxy(&x1, &y1, count);
  124.                 }
  125.         }
  126.     //
  127.         if( 0 == flag )
  128.         {
  129.                 chess[x][y] = 0;
  130.         }
  131.         return 0;
  132. }
  133. int main()
  134. {
  135.         int i, j;
  136.         clock_t start, finish;
  137.         start = clock();
  138.         for ( i=0; i < X; i++ )
  139.         {
  140.                 for ( j=0; j < Y; j++ )
  141.                 {
  142.                         chess[i][j] = 0;
  143.                 }
  144.         }
  145.         if( !TravelChessBoard(2, 0, 1) )
  146.         {
  147.                 printf("马踏棋盘失败~\n");
  148.         }
  149.         finish = clock();
  150.         printf("总共耗时%f秒\n\n", (double)(finish-start)/CLOCKS_PER_SEC);
  151.         return 0;
  152. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-15 07:43:47 | 显示全部楼层
flag没有进入循环,或者nextxy函数返回值为0(具体函数参见nextxy函数)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-15 16:54:14 From FishC Mobile | 显示全部楼层
是不是走到某一个位置不可走了就返回到上一个位置,return 0即112行if语句不被执行接着执行116到123行去选择其他方位
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 06:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表