zoukankan      html  css  js  c++  java
  • 计蒜客中国象棋——搜索DFS

    #include<iostream>
    
    using namespace std;
    
    char g[10][9];
    bool st[10][9];
    
    int dx[]={-2,-1,1,2,2,1,-1,-2},dy[]={1,2,2,1,-1,-2,-2,-1};//这里我是看图从左上顺时针写的
    
    bool dfs(int x,int y)
    {
      st[x][y]=true;
      if(g[x][y]=='T')return true;
      
      for(int i=0;i<8;i++)
      {
        int tx=dx[i]+x,ty=dy[i]+y;
        if(tx<0||tx>=10||ty<0||ty>=9)continue;
        if(st[tx][ty])continue;
        if(g[tx][ty]=='#')continue;
        if(dfs(tx,ty)==true)return true;//这里必须加递归判断,因为我们是找能不能走到,
        //如果不加if true的话,在否定的情况下就相当于提前返回了
        //必须在这里把8个预设坐标都判断一下,能到就表明到得了,
        //8个坐标递归之后的逻辑也是如
    } return false; } int main() { for(int i=0;i<10;i++)cin>>g[i]; for(int i=0;i<10;i++) for(int j=0;j<9;j++) if(g[i][j]=='S') if(dfs(i,j))cout<<"Yes"; else cout<<"No"; return 0; }

    https://www.jisuanke.com/course/6497/341076

  • 相关阅读:
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    MyDAL
    ThinkPad 安装 Ubuntu 18.10 系统 -- 高分屏各项配置与Nvdia独显驱动
  • 原文地址:https://www.cnblogs.com/h694879357/p/13691115.html
Copyright © 2011-2022 走看看