鱼C论坛

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

关于骑士周游(马踏棋盘)的问题

[复制链接]
发表于 2019-2-11 22:25:48 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 蘼芜 于 2019-2-11 22:30 编辑
  1. #include<stdio.h>
  2. #include<time.h>

  3. #define X 5
  4. #define Y 5

  5. void copychess(int nowchess[X][Y], int chess[X][Y])//复制棋盘
  6. {
  7.         int i, j;
  8.         for (i = 0; i < X; i++)
  9.         {
  10.                 for (j = 0; j < Y; j++)
  11.                 {
  12.                         nowchess[i][j] = chess[i][j];
  13.                 }
  14.         }
  15. }

  16. void printchess(int chess[X][Y])
  17. {
  18.         int i, j;
  19.         for (i = 0; i < X; i++)
  20.         {
  21.                 for (j = 0; j < Y; j++)
  22.                 {
  23.                         printf("%02d        ", chess[i][j]);
  24.                 }
  25.                 putchar('\n');
  26.         }
  27.         printf("****************************************************************************************************\n\n");
  28. }

  29. void travel(int chess[X][Y], int x, int y,int count)
  30. {
  31.         int nowchess[X][Y];
  32.         copychess(nowchess, chess);
  33.         int i, j;
  34.         //调试
  35.         //printf("第%d次递归", count);
  36.         if (count > X*Y)
  37.         {
  38.                 printchess(chess);
  39.         }
  40.         else
  41.         {
  42.                 nowchess[x][y] = count;
  43.                 for (i = 0; i < X; i++)
  44.                 {
  45.                         for (j = 0; j < Y; j++)
  46.                         {
  47.                                 if (nowchess[i][j] == 0)
  48.                                 {
  49.                                         if ((i - x)*(i - x) == 4 && (j - y)*(j - y) == 1)
  50.                                         {
  51.                                                 travel(nowchess, i, j, count + 1);
  52.                                         }
  53.                                         else if ((i - x)*(i - x) == 1 && (j - y)*(j - y) == 4)
  54.                                         {
  55.                                                 travel(nowchess, i, j, count + 1);
  56.                                         }
  57.                                 }
  58.                         }
  59.                 }
  60.         }
  61. }

  62. int main(void)
  63. {
  64.         clock_t start, end;
  65.         int x, y;
  66.         int chess[X][Y] = { 0 };
  67.         printchess(chess);
  68.         //scanf_s("%d,%d", &x, &y);
  69.         start = clock();
  70.         for (x = 0; x < X; x++)
  71.         {
  72.                 for (y = 0; y < Y; y++)
  73.                 {
  74.                         travel(chess, x, y, 1);
  75.                 }
  76.         }
  77.         //travel(chess, x, y, 1);
  78.         end = clock();
  79.         printf("The program has run: %f s.\n", (double)((end - start) / CLOCKS_PER_SEC));
  80.         getchar();
  81.         getchar();
  82.         return 0;
  83. }
复制代码


算是纯递归的,8*8的单点好久算不出,认了,5*5的算全部解法出错(无解),想请教下这是哪里出问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-13 14:10:06 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 19:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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