Chinaunix首页 | 论坛 | 博客
  • 博客访问: 369188
  • 博文数量: 159
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 182
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-02 10:42
文章分类

全部博文(159)

文章存档

2015年(18)

2014年(132)

2013年(9)

分类: LINUX

2014-07-07 17:52:10

原文地址:马踏棋盘的实现 作者:gongping11

马踏棋盘是经典的程序设计问题之一,主要的解决方案有两种:一种是基于深度优先搜索的方法,另一种是基于贪婪算法的方法。第一种基于深度优先搜索的方法是比较常用的算法,深度优先搜索算法也是数据结构中的经典算法之一,主要是采用递归的思想,一级一级的寻找,最后找到合适的解。而基于贪婪的算法则是依据贪婪算法的思想设置一种标准,然后依据标准进行选择,从而得到解,但是他不一定能够得到最优解。
 
关于马踏棋盘的基本过程:国际象棋的棋盘为8*8的方格棋盘。现将"马"放在任意指定的方格中,按照"马"走棋的规则将"马"进行移动。要求每个方格只能进入一次,最终使得"马"走遍棋盘的64个方格。
 
深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. (来自百度)

贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。贪心算法不是对所有问题都能得到整体最优解,但对范围相当广泛的许多问题他能产生整体最优解或者是整体最优解的近似解。(来自百度)

其中基于深度优先搜索的算法就是依据当前点找到下一个可能的点,然后对这个点进行深度优先搜索,然后依次递归,当出现条件不满足时,退回来,采用其他的路劲进行搜索,最后肯定能够得到对应的结果。实现的基本过程如下:

  1. /*deepsearch to solve the horse chess problem*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. #define ROWS    8
  6. #define COLS    8

  7. int chess[ROWS][COLS];

  8. /*eight directions of x moved*/
  9. const int x_move[] = {-2,-1,1,2,2,1,-1,-2};
  10. /*eight directions of y moved*/
  11. const int y_move[] = {1,2,2,1,-1,-2,-2,-1};

  12. void print_matrix()
  13. {
  14.     int i = 0,j = 0;
  15.     for (i = 0; i < ROWS; ++ i)
  16.     {
  17.         for (j = 0; j < COLS; ++ j)
  18.         {
  19.             printf("%d\t",chess[i][j]);
  20.         }
  21.         printf("\n\n\n");
  22.     }
  23. }

  24. /*find the next point*/
  25. int nextxy(int *x,int *y,int count)
  26. {
  27.     if(count > 7 && count < 0)
  28.         return -1;
  29.     switch(count)
  30.     {
  31.     case 0:
  32.         /*check the conditions*/
  33.         if(*x + x_move[0] < ROWS &&
  34.          *x + x_move[0]>= 0 &&
  35.          *y + y_move[0]< COLS &&
  36.          *y + y_move[0]>= 0 &&
  37.          chess[*x + x_move[0]][*y + y_move[0]] == 0)
  38.         {
  39.             *x += x_move[0];
  40.             *y += y_move[0];
  41.             break;
  42.         }
  43.         else/*failed*/
  44.             return 0;
  45.     case 1:
  46.         if(*x + x_move[1] < ROWS &&
  47.             *x + x_move[1]>= 0 &&
  48.             *y + y_move[1]< COLS &&
  49.             *y + y_move[1]>= 0 &&
  50.          chess[*x + x_move[1]][*y + y_move[1]] == 0)
  51.         {
  52.             *x += x_move[1];
  53.             *y += y_move[1];
  54.             break;
  55.         }
  56.         else
  57.             return 0;
  58.     case 2:
  59.         if(*x + x_move[2] < ROWS &&
  60.             *x + x_move[2]>= 0 &&
  61.             *y + y_move[2]< COLS &&
  62.             *y + y_move[2]>= 0 &&
  63.          chess[*x + x_move[2]][*y + y_move[2]] == 0)
  64.         {
  65.             *x += x_move[2];
  66.             *y += y_move[2];
  67.             break;
  68.         }
  69.         else
  70.             return 0;
  71.     case 3:
  72.         if(*x + x_move[3] < ROWS &&
  73.             *x + x_move[3]>= 0 &&
  74.             *y + y_move[3]< COLS &&
  75.             *y + y_move[3]>= 0 &&
  76.          chess[*x + x_move[3]][*y + y_move[3]] == 0)
  77.         {
  78.             *x += x_move[3];
  79.             *y += y_move[3];
  80.             break;
  81.         }
  82.         else
  83.             return 0;
  84.     case 4:
  85.         if(*x + x_move[4] < ROWS &&
  86.             *x + x_move[4]>= 0 &&
  87.             *y + y_move[4]< COLS &&
  88.             *y + y_move[4]>= 0 &&
  89.          chess[*x + x_move[4]][*y + y_move[4]] == 0)
  90.         {
  91.             *x += x_move[4];
  92.             *y += y_move[4];
  93.             break;
  94.         }
  95.         else
  96.             return 0;
  97.     case 5:
  98.         if(*x + x_move[5] < ROWS &&
  99.             *x + x_move[5]>= 0 &&
  100.             *y + y_move[5]< COLS &&
  101.             *y + y_move[5]>= 0 &&
  102.          chess[*x + x_move[5]][*y + y_move[5]] == 0)
  103.         {
  104.             *x += x_move[5];
  105.             *y += y_move[5];
  106.             break;
  107.         }
  108.         else
  109.             return 0;
  110.     case 6:
  111.         if(*x + x_move[6] < ROWS &&
  112.             *x + x_move[6]>= 0 &&
  113.             *y + y_move[6]< COLS &&
  114.             *y + y_move[6]>= 0 &&
  115.             chess[*x + x_move[6]][*y + y_move[6]] == 0)
  116.         {
  117.             *x += x_move[6];
  118.             *y += y_move[6];
  119.             break;
  120.         }
  121.         else
  122.             return 0;
  123.     case 7:
  124.         if(*x + x_move[7] < ROWS &&
  125.             *x + x_move[7]>= 0 &&
  126.             *y + y_move[7]< COLS &&
  127.             *y + y_move[7]>= 0 &&
  128.          chess[*x + x_move[7]][*y + y_move[7]] == 0)
  129.         {
  130.             *x += x_move[7];
  131.             *y += y_move[7];
  132.             break;
  133.         }
  134.         else
  135.             return 0;
  136.     default:
  137.         return 0;
  138.     }
  139.     return 1;
  140. }
  141. int deepsearch(int x,int y, int j)
  142. {
  143.     /*save the value x,y*/
  144.     int x1 = x, y1 = y;
  145.     int tag = 0, i = 0;
  146.     /*save j on chess[x][y]*/
  147.     chess[x][y] = j;

  148.     /*recursion exit condition*/
  149.     if(j == COLS*ROWS)
  150.     {
  151.         return 1;
  152.     }
  153.     /*find the next point in eight directions*/
  154.     tag = nextxy(&x1,&y1,i);
  155.     /*find the nextx,nexty */
  156.     while (tag == 0 && i < 7)
  157.     {
  158.         i ++;
  159.         tag = nextxy(&x1,&y1, i);
  160.     }

  161.     /*the nextxy be found*/
  162.     while(tag)
  163.     {
  164.         if(deepsearch(x1,y1,j+1))
  165.             return 1;

  166.      /*if failed, a new finding process */
  167.         x1 = x; y1 = y;
  168.         i ++;
  169.         tag = nextxy(&x1,&y1,i);
  170.         while (tag == 0 && i < 7)
  171.         {
  172.             i ++;
  173.             tag = nextxy(&x1,&y1,i);
  174.         }
  175.     }
  176.     /*no direction can find next point*/
  177.     if(tag == 0)
  178.         chess[x][y] = 0;
  179.     return 0;
  180. }

  181. void main()
  182. {
  183.     clock_t start = clock();
  184.     deepsearch(2,0,1);
  185.     print_matrix();
  186.     int seconds = (clock()-start)/CLOCKS_PER_SEC;

  187.     printf("\n%d\n",seconds);
  188.     return;
  189. }

采用贪婪算法的实现,首先应该设置一定的判断准则,然后根据设定的准则进行选择,在马踏棋盘中设定的准备是选择出路最少(至少为1)的一个方向为准则,能够实现马踏棋盘问题的解决。当然该问题为什么要选择出路最少,我暂时还不是很清楚。基本的实现如下:

点击(此处)折叠或打开

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

  4. #define ROWS    8
  5. #define COLS    8

  6. /*axis i move matrix*/
  7. const int iktmove[8] = {-2,-1,1,2,2,1,-1,-2};
  8. /*axis j move matrix*/
  9. const int jktmove[8] = {1,2,2,1,-1,-2,-2,-1};

  10. int board[ROWS][COLS];

  11. /*inital the matrix*/
  12. void matrix_init(int matrix[][COLS],int rows,int cols)
  13. {
  14.     int i, j;
  15.     for(i = 0; i < rows ; ++ i)
  16.         for (j = 0; j < cols ; ++ j)
  17.         {
  18.             matrix[i][j] = 0;
  19.         }
  20. }
  21. /*print the matrix*/
  22. void print_matrix(int matrix[][COLS],int rows,int cols)
  23. {
  24.     int i,j;
  25.     for(i = 0; i < rows; ++ i)
  26.     {
  27.         for(j = 0; j < cols; ++ j)
  28.             printf("%d\t",matrix[i][j]);
  29.         printf("\n\n\n");
  30.     }
  31. }
  32. /*find the index of min non-zeros positive*/
  33. int minindex_in_matrix(int a[],int cols)
  34. {
  35.     int i = 0,index = 0;
  36.     int min = a[0];
  37.     for(i = 0 ; i< cols; ++ i)
  38.     {
  39.         if(a[i] >0)
  40.         {
  41.             min = a[i];
  42.             index = i;
  43.             break;
  44.         }
  45.     }
  46.     for(i = index + 1; i < cols ; ++ i)
  47.     {
  48.         if(a[i] > 0 && min > a[i])
  49.         {
  50.             min = a[i];
  51.             index = i;
  52.         }
  53.     }
  54.     if(a[index] > 0)
  55.         return index;
  56.     return -1;
  57. }
  58. int maxindex_in_matrix(int a[],int cols)
  59. {
  60.     int i = 0,index;
  61.     int max ;
  62.     for(i = 0 ; i< cols; ++ i)
  63.     {
  64.         if(a[i] >0)
  65.         {
  66.             max = a[i];
  67.             index = i;
  68.             break;
  69.         }
  70.     }
  71.     for(i = index + 1; i < cols ; ++ i)
  72.     {
  73.         if(a[i] > 0 && max < a[i])
  74.         {
  75.             max = a[i];
  76.             index = i;
  77.         }
  78.     }
  79.     if(a[index] > 0)
  80.         return index;
  81.     return -1;
  82. }

  83. /**/
  84. void warnsdorff_role(int matrix[][COLS],int rows,int cols,int start_i,int start_j)
  85. {
  86.     int i,npos,m,min,j,nnpos;

  87.     int nexti[8] = {0,0,0,0,0,0,0,0};
  88.     int nextj[8] = {0,0,0,0,0,0,0,0};
  89.     int exit[8] = {0,0,0,0,0,0,0,0};
  90.     
  91.     /*steps a*/
  92.     matrix_init(matrix,rows,cols);
  93.     /*steps b*/
  94.     matrix[start_i][start_j] = 1;
  95.     /*steps c*/
  96.     for(m = 1; m < 64; ++ m)
  97.     {
  98.         /*steps d*/
  99.         npos = 0;
  100.         for(i = 0; i < 8; ++ i)
  101.         {
  102.             /*ignore the point which doesn't satisfy the conditions*/
  103.             if( start_i + iktmove[i] < 0 ||
  104.                 start_i + iktmove[i] >= rows ||
  105.                 start_j + jktmove[i] < 0 ||
  106.                 start_j + jktmove[i] >= cols ||
  107.                 matrix[start_i + iktmove[i]][start_j+jktmove[i]] > 0)
  108.             {
  109.                 continue;
  110.             }
  111.             /*save the point which satisfy the conditions*/
  112.             nexti[npos] = start_i + iktmove[i];
  113.             nextj[npos] = start_j + jktmove[i];
  114.             /*statistics how many point satisfy conditions*/
  115.             npos ++;
  116.         }
  117.         /*steps e*/
  118.         if(npos == 0)
  119.         {
  120.             printf("Can not finish the game!!\n,The steps of game can be %d\n",m);    
  121.             goto print;
  122.         }
  123.         /*steps e*/
  124.         if(npos == 1)
  125.         {
  126.             min = 0;
  127.             goto set_nextpoint;
  128.         }
  129.         /*steps f*/
  130.         if(npos > 1)
  131.         {
  132.             /*calculate the possible way of the next next step */
  133.             for(i = 0; i < npos ; ++ i)
  134.             {
  135.                 nnpos = 0;
  136.                 for(j = 0; j < 8; ++ j)
  137.                 {
  138.                     /*statistics the point which satisfy conditions*/
  139.                     if( nexti[i] + iktmove[j] >= 0 &&
  140.                         nexti[i] + iktmove[j] < rows &&
  141.                         nextj[i] + jktmove[j] >= 0 &&
  142.                         nextj[i] + jktmove[j] < cols &&
  143.                         matrix[nexti[i] + iktmove[j]][nextj[i] + jktmove[j]] == 0)
  144.                     {
  145.                         nnpos ++;
  146.                     }
  147.                 }
  148.                 /*save the numbers of points for next step*/
  149.                 exit[i] = nnpos;
  150.             }
  151.             /*find the proper point*/
  152.             if((min = minindex_in_matrix(exit,npos)) >= 0)
  153.             {
  154.                 goto set_nextpoint;
  155.             }
  156.             else /*failed*/
  157.             {
  158.                 printf("Can not finish the game!!\n,The steps of game can be %d\n",m);
  159.                 goto print;
  160.             }
  161.         }
  162. set_nextpoint:
  163.         /*step g*/
  164.         /*set the next start point of game*/
  165.         start_i = nexti[min];
  166.         start_j = nextj[min];
  167.         matrix[start_i][start_j] = m+1;
  168.     }
  169. print:
  170.     /*step h*/
  171.     print_matrix(matrix,rows,cols);
  172. }

  173. void main()
  174. {
  175.     warnsdorff_role(board,ROWS,COLS,5,1);
  176. }

实现结果:当采用深度优先搜索算法的过程中发现当棋盘为8X8时,计算速度非常的慢,而当棋盘为5X5以后,计算速度非常的快速。说明算法存在一定的缺陷。而采用贪婪算法实现的速度非常的快,棋盘的大小对算法性能影响较小。

将矩阵改为5X5得到如下的结果:从结果中可以看见两种算法得到的结果存在一定的差异,但是都能解决马踏棋盘问题。

1、深度优先搜索算法:

2、贪婪算法:

 
阅读(728) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~