3 solutions

  • 0
    @ 2023-10-11 16:30:12
    #include<iostream>
    using namespace std;
    #define N 10
    struct point 
    {
        int x ;
        int y;
    };
    point path[N*N];//记录每个格子的坐标,每隔元素是结构体类型
    int n,m,step,cnt;
    int dx[2]={1,0};//方向数组
    int dy[2]={0,1};
    void dfs(int a,int b,int step){
        //终止条件:搜索到终点
        if(a==n&&b==m){ 
          cnt++;
          cout<<cnt<<":";
          for(int i=0;i<step;i++){
            if(path[i].x==n&&path[i].y==m)cout<<path[i].x<<","<<path[i].y;
            else  cout<<path[i].x<<","<<path[i].y<<"->";   
          }
          cout<<n<<","<<m;
          puts("");
          return ;
        }
          for(int i=0;i<2;i++){
            int tx=a+dx[i];
            int ty=b+dy[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                step++;
                path[step].x=tx;
                path[step].y=ty;
                dfs(tx,ty,step);
                step--;  
            }
          }
        }
    int main(){
        path[0].x=path[0].y=1;
        cin>>n>>m;
        dfs(1,1,0);
        return 0;
    }
    
    
    
    
    
    

    Information

    ID
    697
    Time
    1000ms
    Memory
    16MiB
    Difficulty
    5
    Tags
    # Submissions
    247
    Accepted
    88
    Uploaded By