转:数据结构与算法1:马踏棋盘问题(骑士周游问题)

 问题描述:在一个国际象棋的棋盘上,一个马按照它的规则如何才能从一个点出发遍历每一个位置,且每个点只访问一次。

    问题分析:这是一个深搜的问题,沿着一条路前进直到遍历全部的点,那就完成了整个的过程。如果不行,就回退一步,换个方向继续前进。这可以用递归很方便地实现。注意到马在某个位置最多有8个方向可以走,因此需要对这8个方向进行试探。当然这8个方向不一定都存在,比如在边缘可能就会少一些。考虑到实际8*8的棋盘计算量比较大,我们使用6*6代替。

[cpp] 
view plain
copy
《转:数据结构与算法1:马踏棋盘问题(骑士周游问题)》
《转:数据结构与算法1:马踏棋盘问题(骑士周游问题)》

  1. #include <stdio.h>  
  2. #define N 6  
  3.   
  4. int chess[N][N];  
  5.   
  6. int next(int* x, int* y, int dir)  
  7. {  
  8.     switch(dir)  
  9.     {  
  10.     case 1:  
  11.         if (*x+1 < N && *y-2 >= 0 && chess[*x+1][*y-2] == 0)  
  12.         {  
  13.             *x += 1;  
  14.             *y -= 2;  
  15.             return 1;  
  16.         }  
  17.         break;  
  18.     case 2:  
  19.         if (*x+2 < N && *y-1 >= 0 && chess[*x+2][*y-1] == 0)  
  20.         {  
  21.             *x += 2;  
  22.             *y -= 1;  
  23.             return 1;  
  24.         }  
  25.         break;  
  26.     case 3:  
  27.         if (*x+2 < N && *y+1 < N && chess[*x+2][*y+1] == 0)  
  28.         {  
  29.             *x += 2;  
  30.             *y += 1;  
  31.             return 1;  
  32.         }  
  33.         break;  
  34.     case 4:  
  35.         if (*x+1 < N && *y+2 < N && chess[*x+1][*y+2] == 0)  
  36.         {  
  37.             *x += 1;  
  38.             *y += 2;  
  39.             return 1;  
  40.         }  
  41.         break;  
  42.     case 5:  
  43.         if (*x-1 >= 0 && *y+2 < N && chess[*x-1][*y+2] == 0)  
  44.         {  
  45.             *x -= 1;  
  46.             *y += 2;  
  47.             return 1;  
  48.         }  
  49.         break;  
  50.     case 6:  
  51.         if (*x-2 >= 0 && *y+1 < N && chess[*x-2][*y+1] == 0)  
  52.         {  
  53.             *x -= 2;  
  54.             *y += 1;  
  55.             return 1;  
  56.         }  
  57.         break;  
  58.     case 7:  
  59.         if (*x-2 >= 0 && *y-1 >= 0 && chess[*x-2][*y-1] == 0)  
  60.         {  
  61.             *x -= 2;  
  62.             *y -= 1;  
  63.             return 1;  
  64.         }  
  65.         break;  
  66.     case 8:  
  67.         if (*x-1 >= 0 && *y-2 >= 0 && chess[*x-1][*y-2] == 0)  
  68.         {  
  69.             *x -= 1;  
  70.             *y -= 2;  
  71.             return 1;  
  72.         }  
  73.         break;  
  74.     }  
  75.     return 0;  
  76. }  
  77.   
  78. int search(int x, int y, int now)  
  79. {  
  80.     int x1=x, y1=y;  
  81.     chess[x][y] = now;  
  82.     if (now == N*N)  
  83.         return 1;  
  84.     if (next(&x1, &y1, 1))  
  85.     {  
  86.         if (search(x1, x2, now+1))  
  87.         {  
  88.             return 1;  
  89.         }  
  90.     }  
  91.     if (next(&x1, &y1, 2))  
  92.     {  
  93.         if (search(x1, x2, now+1))  
  94.         {  
  95.             return 1;  
  96.         }  
  97.     }  
  98.     if (next(&x1, &y1, 3))  
  99.     {  
  100.         if (search(x1, x2, now+1))  
  101.         {  
  102.             return 1;  
  103.         }  
  104.     }  
  105.     if (next(&x1, &y1, 4))  
  106.     {  
  107.         if (search(x1, x2, now+1))  
  108.         {  
  109.             return 1;  
  110.         }  
  111.     }  
  112.     if (next(&x1, &y1, 5))  
  113.     {  
  114.         if (search(x1, x2, now+1))  
  115.         {  
  116.             return 1;  
  117.         }  
  118.     }  
  119.     if (next(&x1, &y1, 6)  
  120.     {  
  121.         if (search(x1, x2, now+1))  
  122.         {  
  123.             return 1;  
  124.         }  
  125.     }  
  126.     if (next(&x1, &y1, 7))  
  127.     {  
  128.         if (search(x1, x2, now+1))  
  129.         {  
  130.             return 1;  
  131.         }  
  132.     }  
  133.     if (next(&x1, &y1, 8))  
  134.     {  
  135.         if (search(x1, x2, now+1))  
  136.         {  
  137.             return 1;  
  138.         }  
  139.     }  
  140.     chess[x][y] = 0;  
  141.     return 0;  
  142. }  
  143.   
  144. int main()  
  145. {  
  146.     search(0, 0, 1);  
  147.     return 0;  
  148. }  
    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/sxb15929892495/article/details/25845203
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞