鱼C论坛

 找回密码
 立即注册
查看: 2614|回复: 1

马踏棋盘算法怎么我感觉一直死循环啊。。

[复制链接]
发表于 2017-11-27 16:41:18 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>

  5. #define MAX_VERTEX_NUM 10
  6. #define X 5
  7. #define Y 5        
  8. int chess[X][Y];

  9. int nextxy(int *x, int *y, int count)
  10. {
  11.         switch (count)
  12.         {
  13.         case 0:
  14.                 if (*x + 2 <= X - 1 && *y - 1 >= 0 && chess[*x + 2][*y - 1] == 0)
  15.                 {
  16.                         *x = *x + 2;
  17.                         *y = *y - 1;
  18.                         return 1;
  19.                 }
  20.                 break;

  21.         case 1:
  22.                 if (*x + 2 <= X - 1 && *y + 1 <= Y - 1 && chess[*x + 2][*y + 1] == 0)
  23.                 {
  24.                         *x = *x + 2;
  25.                         *y = *y + 1;
  26.                         return 1;
  27.                 }
  28.                 break;

  29.         case 2:
  30.                 if (*x + 1 <= X - 1 && *y - 2 >= 0 && chess[*x + 1][*y - 2] == 0)
  31.                 {
  32.                         *x = *x + 1;
  33.                         *y = *y - 2;
  34.                         return 1;
  35.                 }
  36.                 break;

  37.         case 3:
  38.                 if (*x + 1 <= X - 1 && *y + 2 <= Y - 1 && chess[*x + 1][*y + 2] == 0)
  39.                 {
  40.                         *x = *x + 1;
  41.                         *y = *y + 2;
  42.                         return 1;
  43.                 }
  44.                 break;

  45.         case 4:
  46.                 if (*x - 2 >= 0 && *y - 1 >= 0 && chess[*x - 2][*y - 1] == 0)
  47.                 {
  48.                         *x = *x - 2;
  49.                         *y = *y - 1;
  50.                         return 1;
  51.                 }
  52.                 break;

  53.         case 5:
  54.                 if (*x - 2 >= 0 && *y + 1 <= Y - 1 && chess[*x - 2][*y + 1] == 0)
  55.                 {
  56.                         *x = *x - 2;
  57.                         *y = *y + 1;
  58.                         return 1;
  59.                 }
  60.                 break;

  61.         case 6:
  62.                 if (*x - 1 >= 0 && *y - 2 >= 0 && chess[*x - 1][*y - 2] == 0)
  63.                 {
  64.                         *x = *x - 1;
  65.                         *y = *y - 2;
  66.                         return 1;
  67.                 }
  68.                 break;

  69.         case 7:
  70.                 if (*x - 1 >= 0 && *y + 2 <= Y - 1 && chess[*x - 1][*y + 2] == 0)
  71.                 {
  72.                         *x = *x - 1;
  73.                         *y = *y + 2;
  74.                         return 1;
  75.                 }
  76.                 break;

  77.         default:
  78.                 break;
  79.         }

  80.         return 0;
  81. }



  82. void print() {
  83.         int i, j;
  84.         for (i = 0; i < X; i++)
  85.         {
  86.                 for (j = 0; j < Y; j++)
  87.                 {
  88.                         printf("%2d\t", chess[i][j]);

  89.                 }
  90.                 printf("\n");
  91.         }
  92.         printf("\n");
  93. }

  94. long h = 0;

  95. int travechess(int x, int y, int tag) {
  96.         printf("当前外层x:%d,当前外层y:%d,当前tag:%d\n",x, y, tag);
  97.         chess[x][y] = tag;
  98.         int x1 = x, y1 = y, flag = 0,count=0;

  99.         if (tag == X*Y)
  100.         {
  101.                 print();
  102.                 return 1;
  103.         }

  104.         flag = nextxy(&x1, &y1, count);
  105.         while (count<7 && flag==0)
  106.         {
  107.                 count++;
  108.                 flag = nextxy(&x1, &y1, count);
  109.         }

  110.         while (flag==1)
  111.         {
  112.                 if (travechess(x1, y1, tag + 1)) {
  113.                         return 1;
  114.                 };
  115.                 count++;
  116.                 x1 = x;
  117.                 y1 = y;
  118.                 flag = nextxy(&x1, &y1, count);
  119.                 while (count<7 && flag == 0)
  120.                 {
  121.                         count++;
  122.                         flag = nextxy(&x1, &y1, count);
  123.                 }

  124.         }

  125.         if (flag==0)
  126.         {
  127.                 chess[x][y] =0;
  128.         }


  129.         return 0;


  130. }





  131. int main() {
  132.         int i, j;
  133.         clock_t start, finish;
  134.         start = clock();
  135.         for (i = 0; i < X; i++)
  136.         {
  137.                 for (j = 0; j < Y; j++)
  138.                 {
  139.                         chess[i][j] = 0;
  140.                 }

  141.         }
  142.         if (!travechess(2, 0, 1))
  143.         {
  144.                 printf("bao qian,shi bai le");
  145.         }
  146.         finish = clock();
  147.         printf("\nben ci ji suan shi jian:%fmiao\n\n", (double)((finish - start) /CLOCKS_PER_SEC));



  148.          return 0;


  149.        


  150. }
复制代码


看了小甲鱼的马踏棋盘算法,然后模仿着自己写了一个,完全自己写的,然后感觉很多时候都是死循环,一直没结果。。。就算5*5的棋盘都是,有时又有结果,怎么回事啊,我电脑是7700k,4.5g,的cpu,应该很快啊,这种算法都这么慢啊!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-27 18:28:42 From FishC Mobile | 显示全部楼层
有人吗?鱼油呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-16 14:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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