<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 馬踏棋盤的實(shí)現(xiàn)

          馬踏棋盤的實(shí)現(xiàn)

          作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏

          采用貪婪算法的實(shí)現(xiàn),首先應(yīng)該設(shè)置一定的判斷準(zhǔn)則,然后根據(jù)設(shè)定的準(zhǔn)則進(jìn)行選擇,在馬踏棋盤中設(shè)定的準(zhǔn)備是選擇出路最少(至少為1)的一個(gè)方向?yàn)闇?zhǔn)則,能夠?qū)崿F(xiàn)馬踏棋盤問題的解決。當(dāng)然該問題為什么要選擇出路最少,我暫時(shí)還不是很清楚?;镜膶?shí)現(xiàn)如下:

          #include
          #include
          #include

          #define ROWS 8
          #define COLS 8

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

          int board[ROWS][COLS];

          /*inital the matrix*/
          voidmatrix_init(int matrix[][COLS],int rows,int cols)
          {
          int i, j;
          for(i = 0; i < rows ; ++ i)
          for (j = 0; j < cols ; ++ j)
          {
          matrix[i][j] = 0;
          }
          }
          /*print the matrix*/
          void print_matrix(int matrix[][COLS],int rows,int cols)
          {
          int i,j;
          for(i = 0; i < rows; ++ i)
          {
          for(j = 0; j < cols; ++ j)
          printf("%d ",matrix[i][j]);
          printf("");
          }
          }
          /*find the index of min non-zeros positive*/
          int minindex_in_matrix(int a[],int cols)
          {
          int i = 0,index = 0;
          int min = a[0];
          for(i = 0 ; i< cols; ++ i)
          {
          if(a[i] >0)
          {
          min = a[i];
          index = i;
          break;
          }
          }
          for(i = index + 1; i < cols ; ++ i)
          {
          if(a[i] > 0 && min > a[i])
          {
          min = a[i];
          index = i;
          }
          }
          if(a[index] > 0)
          return index;
          return -1;
          }
          int maxindex_in_matrix(int a[],int cols)
          {
          int i = 0,index;
          int max ;
          for(i = 0 ; i< cols; ++ i)
          {
          if(a[i] >0)
          {
          max = a[i];
          index = i;
          break;
          }
          }
          for(i = index + 1; i < cols ; ++ i)
          {
          if(a[i] > 0 && max < a[i])
          {
          max = a[i];
          index = i;
          }
          }
          if(a[index] > 0)
          return index;
          return -1;
          }

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

          int nexti[8] = {0,0,0,0,0,0,0,0};
          intnextj[8] = {0,0,0,0,0,0,0,0};
          int exit[8] = {0,0,0,0,0,0,0,0};

          /*steps a*/
          matrix_init(matrix,rows,cols);
          /*steps b*/
          matrix[start_i][start_j] = 1;
          /*steps c*/
          for(m = 1; m < 64; ++ m)
          {
          /*steps d*/
          npos = 0;
          for(i = 0; i < 8; ++ i)
          {
          /*ignore the point which doesnt satisfy the conditions*/
          if( start_i + iktmove[i] < 0 ||
          start_i + iktmove[i] >= rows ||
          start_j + jktmove[i] < 0 ||
          start_j + jktmove[i] >= cols ||
          matrix[start_i + iktmove[i]][start_j+jktmove[i]] > 0)
          {
          continue;
          }
          /*save the point which satisfy the conditions*/
          nexti[npos] = start_i + iktmove[i];
          nextj[npos] = start_j + jktmove[i];
          /*statistics how many point satisfy conditions*/
          npos ++;
          }
          /*steps e*/
          if(npos == 0)
          {
          printf("Can notfinishthegame!!,The steps of game can be %d",m);
          goto print;
          }
          /*steps e*/
          if(npos == 1)
          {
          min = 0;
          goto set_nextpoint;
          }
          /*steps f*/
          if(npos > 1)
          {
          /*calculate the possible way of the next next step */
          for(i = 0; i < npos ; ++ i)
          {
          nnpos = 0;
          for(j = 0; j < 8; ++ j)
          {
          /*statistics the point which satisfy conditions*/
          if(nexti[i] + iktmove[j] >= 0 &&
          nexti[i] + iktmove[j] < rows &&
          nextj[i] + jktmove[j] >= 0 &&
          nextj[i] + jktmove[j] < cols &&
          matrix[nexti[i] + iktmove[j]][nextj[i] + jktmove[j]] == 0)
          {
          nnpos ++;
          }
          }
          /*save the numbers of points fornextstep*/
          exit[i] = nnpos;
          }
          /*find the proper point*/
          if((min = minindex_in_matrix(exit,npos)) >= 0)
          {
          goto set_nextpoint;
          }
          else /*failed*/
          {
          printf("Can notfinishthe game!!,The steps of game can be %d",m);
          goto print;
          }
          }
          set_nextpoint:
          /*step g*/
          /*set the next start point ofgame*/
          start_i = nexti[min];
          start_j =nextj[min];
          matrix[start_i][start_j] = m+1;
          }
          print:
          /*step h*/
          print_matrix(matrix,rows,cols);
          }

          void main()
          {
          warnsdorff_role(board,ROWS,COLS,5,1);
          }

          實(shí)現(xiàn)結(jié)果:當(dāng)采用深度優(yōu)先搜索算法的過程中發(fā)現(xiàn)當(dāng)棋盤為8X8時(shí),計(jì)算速度非常的慢,而當(dāng)棋盤為5X5以后,計(jì)算速度非常的快速。說明算法存在一定的缺陷。而采用貪婪算法實(shí)現(xiàn)的速度非常的快,棋盤的大小對(duì)算法性能影響較小。

          將矩陣改為5X5得到如下的結(jié)果:從結(jié)果中可以看見兩種算法得到的結(jié)果存在一定的差異,但是都能解決馬踏棋盤問題。

          1、深度優(yōu)先搜索算法:

          2、貪婪算法:


          上一頁(yè) 1 2 下一頁(yè)

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();