吃奥利给咳嗽 2021-10-18 21:30 采纳率: 100%
浏览 225
已结题

跳马问题的matlab代码实现

中国象棋棋盘有10行9列,我们需要计算马走到每个点的步数,并将结果储存在10*9的矩阵中,起点键入 需要具体代码

  • 写回答

1条回答 默认 最新

  • 胸毛男 2021-10-18 23:52
    关注

    大概是这样:
    函数脚本:

    
    %y_row
    %x_col
    function HorseGo(y,x,ylast,xlast, n)
    global BigA;
    %如果在棋盘外 结束
    if((x<1 ||x>9 || y<1 ||y>10))
        return;
    end
        
    n=n+1;
    
    %八种跳法
    xtemp = 0;
    ytemp = 0;
    fuzhiFlag = 0;
    %左上
    xtemp = x-2;
    ytemp = y-1;
    
    % (算完的坐标等于上把的位置 否则就又跳回去了) || (这么跳 新位置在棋盘外)||((此处现有值更小)&& (此处值不为0)))
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    
    %左下
    xtemp = x-2;
    ytemp = y+1;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    %上左
    xtemp = x-1;
    ytemp = y-2;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    %上右
    xtemp = x+1;
    ytemp = y-2;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    
    %右上
    xtemp = x+2;
    ytemp = y-1;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    
    %右下
    xtemp = x+2;
    ytemp = y+1;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    
    %下左
    xtemp = x-1;
    ytemp = y+2;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
    
    %下右
    xtemp = x+1;
    ytemp = y+2;
    if( (ylast==ytemp && xtemp==xlast)||(xtemp<1 ||xtemp>9 || ytemp<1 ||ytemp>10) || (BigA(ytemp,xtemp)<=n && BigA(ytemp,xtemp)~= 0))
        
    else
        BigA(ytemp,xtemp) = n;
        fuzhiFlag=1;
    end
        
        if(fuzhiFlag==0)
            return;
        end;
      %如果都非零
      if(all(BigA))
          return;
      else
          
    %左上      
    xtemp1 = x-2;
    ytemp1 = y-1;
    %左下
    xtemp2 = x-2;
    ytemp2 = y+1;
    %上左
    xtemp3 = x-1;
    ytemp3 = y-2;
    %上右
    xtemp4 = x+1;
    ytemp4 = y-2;
    %右上
    xtemp5 = x+2;
    ytemp5 = y-1;
    %右下
    xtemp6 = x+2;
    ytemp6 = y+1;
    %下左
    xtemp7 = x-1;
    ytemp7 = y+2;
    %下右
    xtemp8 = x+1;
    ytemp8 = y+2;
    
        HorseGo(ytemp1, xtemp1,y,x,n);
        HorseGo(ytemp2,xtemp2, y,x,n);
        HorseGo(ytemp3,xtemp3, y,x,n);
        HorseGo(ytemp4, xtemp4,y,x,n);
        HorseGo(ytemp5, xtemp5,y,x,n);
        HorseGo(ytemp6, xtemp6,y,x,n);
        HorseGo(ytemp7,xtemp7, y,x,n);
        HorseGo(ytemp8,xtemp8,y,x, n);
    end
    
    
    
    

    调用函数的脚本

    
    %象棋 马走法
    global BigA;
    BigA = zeros(10,9);
    % 马的初始位置
    InitPos_y_row = 1;
    InitPos_x_col = 2;
    
    HorseGo(InitPos_y_row,InitPos_x_col,-9999,-999,0);
    %初始位置置0
    BigA(InitPos_y_row,InitPos_x_col)=0;
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月27日
  • 已采纳回答 10月19日
  • 创建了问题 10月18日

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致