全部评论 5

  • 古老的朋友:他人代码

    2024-08-27 来自 广东

    1
  • 此题

    #include<bits/stdc++.h>
    using namespace std;
    
    struct node{
        int x,y,step;
    };
    
    int n,m;
    int dir[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    int vis[41][41];
    char a[41][41];
    queue <node> q;
    
    int main(){
        cin >> n >> m;
        for(int i = 0;i <= n;i++){
            scanf("%s",a[i]);
        }
        int step;
        q.push({0,0,0});
        while(!q.empty()){
            node t = q.front();
            q.pop();
            if(t.x == n - 1 && t.y == m - 1){
                cout << t.step + 1;
                break;
            }
            for(int i = 0;i < 4;i++){
                int tx = t.x + dir[i][0];
                int ty = t.y + dir[i][1];
                if(tx < 0 || tx > n - 1 || ty < 0 || ty > m - 1 || a[tx][ty] == '#' || vis[tx][ty]) continue;
                q.push({tx,ty,t.step + 1});
                vis[tx][ty] = 1;
            }
        }
    	return 0;
    }
    

    送你个模版

    2024-08-26 来自 广东

    0
  • 你回溯时是不是也算了步数

    2024-08-26 来自 广东

    0
  • vis呢?

    2024-08-25 来自 湖南

    0

  • 大佬看看呗

    2024-08-25 来自 广东

    0

热门讨论