1 solutions

  • 0
    @ 2025-11-5 20:17:08

    C :

    #include"stdio.h"
    #define min(x,y) x<y?x:y;
    int map[9][9] = {1,1,1,1,1,1,1,1,1,
                     1,0,0,1,0,0,1,0,1,
                     1,0,0,1,1,0,0,0,1,
                     1,0,1,0,1,1,0,1,1,
                     1,0,0,0,0,1,0,0,1,
                     1,1,0,1,0,1,0,0,1,
                     1,1,0,1,0,1,0,0,1,
                     1,1,0,1,0,0,0,0,1,
                     1,1,1,1,1,1,1,1,1};
    int x2,y2,m;
    int dfs(int x,int y,int s)
    {
        if(map[x][y])return;
        if(x == x2&&y == y2)
        {
            m = min(s,m);
            return;
        }
        s++;
        map[x][y] = 1;
        dfs(x+1,y,s);
        dfs(x,y+1,s);
        dfs(x-1,y,s);
        dfs(x,y-1,s);
        map[x][y] = 0;
    }
    
    int main()
    {
        int x1,y1,n;
        scanf("%d",&n);
        while(n--)
        {
            m = 100;
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
            dfs(x1,y1,0);
            printf("%d\n",m);
        }
        return 0;
    }
    
    

    C++ :

    #include<cstdio>
    #define min(x,y) x>y?y:x
    int maze[9][9]={1,1,1,1,1,1,1,1,1,
                    1,0,0,1,0,0,1,0,1,
                    1,0,0,1,1,0,0,0,1,
                    1,0,1,0,1,1,0,1,1,
                    1,0,0,0,0,1,0,0,1,
                    1,1,0,1,0,1,0,0,1,
                    1,1,0,1,0,1,0,0,1,
                    1,1,0,1,0,0,0,0,1,
                    1,1,1,1,1,1,1,1,1};
    int a,b,c,d,m;
    void dfs(int x,int y,int s){
      if(maze[x][y]) return;
      if(x == c && y == d){
        m = min(s,m);
        return;
      }
      s++;
      maze[x][y]=1;
      dfs(x+1,y,s);
      dfs(x,y+1,s);
      dfs(x-1,y,s);
      dfs(x,y-1,s);
      maze[x][y]=0;
    }
    
    int main(){
      int n;
      scanf("%d",&n);
      while(n--){
        m=9999;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        dfs(a,b,0);
        printf("%d\n",m);
      }
      return 0;
    }
    
    
    • 1

    Information

    ID
    20223
    Time
    3000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By