大佬。请问这题错在哪了
查看原帖
大佬。请问这题错在哪了
559529
SurftheMoon楼主2022/2/10 20:15

样例有3个WA的,看了一下数据是n=1000的情况,但我数组开得挺大了啊....

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=2001;
typedef pair<int,int> PP;
char g[N][N];
int n;
int x1,y3,x2,y2;
bool st[N][N];
int dist[N][N];

int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int bfs(int x1,int y3,int x2,int y2)
{
    
    queue<PP> q;
    q.push({x1,y3});
    st[x1][y3]=true;
    while(!q.empty())
    {
        PP t=q.front();
        q.pop();
        int x=t.first,y=t.second;
        
        if(x==x2&&y==y2)return dist[x][y];
        for(int i=0;i<4;i++)
        {
            int a=x+dx[i],b=y+dy[i];
            if(a>=1&&a<=1001&&b>=1&&b<=1001)
            {
                if(st[a][b])continue;
                if(g[a][b]=='1')continue;
                dist[a][b]=dist[x][y]+1;
                st[a][b]=true;
                q.push({a,b});
            }
        }
    }
   
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>g[i];
    }
    cin>>x1>>y3>>x2>>y2;
    cout<<bfs(x1,y3,x2,y2)<<endl;;
}
2022/2/10 20:15
加载中...