2 solutions

  • 1
    @ 2022-3-24 21:26:55
    #include <stdio.h>
    #include <math.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    using namespace std;
    int n,m,ans=0,vis[25][25],k;
    int dx[4]= {0,-1,0,1},dy[4]= {-1,0,1,0};
    char a[25][25];
    struct ti {
        int start;
        int en;
    };
    ti b[100];
    void dfs(int x,int y,int step) {
        step++;
        b[step].start=x+1;
        b[step].en=y+1;
        if(x==n-1&&y==n-1) {
            if(!k) {
                    cout<<"("<<1<<","<<1<<")";
                for(int i=2; i<=step; i++)
                    cout<<"->"<<"("<<b[i].start<<","<<b[i].en<<")";
                k=1;
            }
    
        }
        vis[x][y]=1;
        for(int i=0; i<4; i++) {
            int tx=x+dx[i],ty=y+dy[i];
            if(tx>=0&&ty>=0&&tx<n&&ty<n&&vis[tx][ty]==0&&a[tx][ty]=='0') {
                dfs(tx,ty,step);
            }
        }
    }
    int main() {
        scanf("%d",&n);
        for(int i=0; i<n; i++) {
            cin>>a[i];
        }
        dfs(0,0,0);
        return 0;
    }
    
    • 0
      @ 2025-1-21 12:12:27

      #include<bits/stdc++.h> using namespace std; const int N=1000; char a[N][N]; int check[N][N]; int m[N][N];
      int n; int dx[4]={0,-1,0,1}; int dy[4]={-1,0,1,0}; void dfs(int nx,int ny,int deep) { deep++; check[nx][ny]=1; m[deep][1]=nx+1; m[deep][2]=ny+1; if(nxn-1&&nyn-1){ for(int j=1;j<=deep;j++){ cout<<"("<<m[j][1]<<","<<m[j][2]<<")"; if(j!=deep){ cout<<"->"; } } } for(int i=0;i<4;i++){ int x=dx[i]+nx; int y=dy[i]+ny; if(check[x][y]0&&x>=0&&x<n&&y>=0&&y<n&&a[x][y]'0'){ dfs(x,y,deep); } } } int main() { int i,j,deep=0; cin>>n; for(i=0;i<n;i++){ for(j=0;j<n;j++){ cin>>a[i][j]; } } dfs(0,0,deep); return 0; }

      • 1

      Information

      ID
      768
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      8
      Tags
      # Submissions
      258
      Accepted
      49
      Uploaded By