输出呢()

P1746 离开中山路

Miracle_InDream @ 2024-01-02 22:58:48

#include<bits/stdc++.h>
using namespace std;
int n;
char mp[1005][1005];
int vis[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int a,b,c,d;
struct node
{
    int x;
    int y;
    int step;
};
node s,t;
queue<node> q;
void bfs()
{
    s.x=a;
    s.y=b;
    t.x=c;
    t.y=d;
    s.step=0;
    q.push(s);
    while(!q.empty())
    {
        node tmp=q.front();
        q.pop();
        vis[tmp.x][tmp.y]=1;
        if(tmp.x==c&&tmp.y==d)
        {
            cout<<tmp.step;
            return;
        }
        for(int i=0;i<4;i++)
        {
            node a;
            a.x=tmp.x+dx[i];
            a.y=tmp.y+dy[i];
            a.step=tmp.step+1;
            if(a.x>0&&a.y>0&&a.x<=n&&a.y<=n&&vis[a.x][a.y]==0&&mp[a.x][a.y]==0)
            {
                q.push(a);
            }
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>mp[i][j];
        }
    }
    cin>>a>>b>>c>>d;
    bfs();
    return 0;
}

by Miracle_InDream @ 2024-01-02 23:12:15

@ISU152_YYDS 被我修好了这处之后提交发生的()


by ICU152_QWQ_IS8 @ 2024-01-02 23:20:03

@shooting__star 尝试把vis判断移到循环里

(手机没法试


by Miracle_InDream @ 2024-01-02 23:21:09

@ISU152_YYDS 不行()


by ICU152_QWQ_IS8 @ 2024-01-02 23:26:42

@shooting__star q pop 下面加 if vis x y continue

(手机胡乱猜结论

MLE肯定是死循环


by Miracle_InDream @ 2024-01-02 23:29:14

@ISU152_YYDS woc AC了()


上一页 |