鱼C论坛

 找回密码
 立即注册
查看: 1043|回复: 2

[已解决]c语音实现马踏棋盘,非递归程序

[复制链接]
发表于 2022-6-7 10:10:00 | 显示全部楼层 |阅读模式

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

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

x
有没有大佬发一下代码
最佳答案
2022-6-7 14:50:03
  1. #include<stdio.h>

  2. int chess[12][12]={0};
  3. int cnt = 0;
  4. int countChess = 0;
  5. int move[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
  6. int initChess();
  7. void printChess();
  8. void horse(int x, int y);
  9. struct Node{
  10.         int x;
  11.         int y;
  12.         int i;
  13. }chessStep[64];
  14. int top = -1;
  15. int main(void){
  16.         initChess();
  17.         int i,j;
  18.         for(i=2; i<10; i++){
  19.                 for(j=2; j<10; j++){
  20.                         cnt = 1;
  21.                         chess[i][j] = cnt;                       
  22.                         horse(i,j);
  23.                 }
  24.         }
  25.        
  26. }
  27. void horse(int x, int y){
  28.         int i = 0;
  29.         while(cnt < 64){
  30.                 int row;
  31.                 int col;
  32.                 for(i; i<8; i++){
  33.                         row = x + move[i][0];
  34.                         col = y + move[i][1];
  35.                         if(chess[row][col] == 0){
  36.                                 break;
  37.                         }
  38.                 }  
  39.                 if(i != 8){
  40.                         chess[row][col] = ++cnt;
  41.                         chessStep[++top].x = x;
  42.                         chessStep[top].y = y;
  43.                         chessStep[top].i = i;
  44.                         if(cnt == 64){
  45.                                 printChess();
  46.                                 chess[row][col] = 0;
  47.                                
  48.                                 i = chessStep[top].i + 1;
  49.                                 x = chessStep[top].x;
  50.                                 y = chessStep[top].y;
  51.                                
  52.                                 top--;
  53.                                 cnt--;
  54.                         }else{
  55.                                 x = row;
  56.                                 y = col;
  57.                                 i = 0;
  58.                         }
  59.                 }else{
  60.                         chess[x][y] = 0;

  61.                         i = chessStep[top].i + 1;
  62.                         x = chessStep[top].x;
  63.                         y = chessStep[top].y;
  64.                                
  65.                         top--;
  66.                         cnt--;
  67.                 }
  68.                 if(top == -1){
  69.                         break;
  70.                 }
  71.         }       
  72. }
  73. int initChess(){
  74.         int i,j;
  75.         for(i=0; i<12; ++i){
  76.                 for(j=0; j<12; ++j){
  77.                         if(i<2 || i>9 || j<2 || j>9){
  78.                                 chess[i][j]=-1;
  79.                         }
  80.                 }
  81.         }
  82. }
  83. void printChess(){
  84.         ++countChess;
  85.         printf("第%d个解是:\n", countChess);
  86.         int i,j;
  87.         for(i=2; i<10; ++i){
  88.                 for(j=2; j<10; ++j){
  89.                         printf("%2d ",chess[i][j]);
  90.                 }
  91.                 printf("\n");
  92.         }
  93. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-7 13:56:17 | 显示全部楼层
踏一步的我会写
踏两步 就可能有64种变化  踏三步就有可能有512种变化  踏四步就有可能有4096种变化
太复杂了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-7 14:50:03 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>

  2. int chess[12][12]={0};
  3. int cnt = 0;
  4. int countChess = 0;
  5. int move[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
  6. int initChess();
  7. void printChess();
  8. void horse(int x, int y);
  9. struct Node{
  10.         int x;
  11.         int y;
  12.         int i;
  13. }chessStep[64];
  14. int top = -1;
  15. int main(void){
  16.         initChess();
  17.         int i,j;
  18.         for(i=2; i<10; i++){
  19.                 for(j=2; j<10; j++){
  20.                         cnt = 1;
  21.                         chess[i][j] = cnt;                       
  22.                         horse(i,j);
  23.                 }
  24.         }
  25.        
  26. }
  27. void horse(int x, int y){
  28.         int i = 0;
  29.         while(cnt < 64){
  30.                 int row;
  31.                 int col;
  32.                 for(i; i<8; i++){
  33.                         row = x + move[i][0];
  34.                         col = y + move[i][1];
  35.                         if(chess[row][col] == 0){
  36.                                 break;
  37.                         }
  38.                 }  
  39.                 if(i != 8){
  40.                         chess[row][col] = ++cnt;
  41.                         chessStep[++top].x = x;
  42.                         chessStep[top].y = y;
  43.                         chessStep[top].i = i;
  44.                         if(cnt == 64){
  45.                                 printChess();
  46.                                 chess[row][col] = 0;
  47.                                
  48.                                 i = chessStep[top].i + 1;
  49.                                 x = chessStep[top].x;
  50.                                 y = chessStep[top].y;
  51.                                
  52.                                 top--;
  53.                                 cnt--;
  54.                         }else{
  55.                                 x = row;
  56.                                 y = col;
  57.                                 i = 0;
  58.                         }
  59.                 }else{
  60.                         chess[x][y] = 0;

  61.                         i = chessStep[top].i + 1;
  62.                         x = chessStep[top].x;
  63.                         y = chessStep[top].y;
  64.                                
  65.                         top--;
  66.                         cnt--;
  67.                 }
  68.                 if(top == -1){
  69.                         break;
  70.                 }
  71.         }       
  72. }
  73. int initChess(){
  74.         int i,j;
  75.         for(i=0; i<12; ++i){
  76.                 for(j=0; j<12; ++j){
  77.                         if(i<2 || i>9 || j<2 || j>9){
  78.                                 chess[i][j]=-1;
  79.                         }
  80.                 }
  81.         }
  82. }
  83. void printChess(){
  84.         ++countChess;
  85.         printf("第%d个解是:\n", countChess);
  86.         int i,j;
  87.         for(i=2; i<10; ++i){
  88.                 for(j=2; j<10; ++j){
  89.                         printf("%2d ",chess[i][j]);
  90.                 }
  91.                 printf("\n");
  92.         }
  93. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 07:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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