求助

P1746 离开中山路

Elliott19 @ 2023-03-18 13:58:12

#include<bits/stdc++.h>
using namespace std;
int sum;
struct node
{
    int x,y,step;
};
queue<node>q;
int f[2100][2100],next[5]={1,-1,1,-1},n;
char a[2100][2100];
int bfs(int x,int y,int x2,int y2)
{
    q.push((node){x,y,0});
    while(!q.empty())
    {
        node u=q.front();
        q.pop();
        int newx=u.x+1;
        if(newx==x2&&u.y==y2) return u.step+1;
        if(newx<=n&&f[newx][u.y]==0)
        {
            f[newx][u.y]=u.step+1;
            q.push((node){newx,u.y,f[newx][u.y]});
        }
        newx=u.x-1;
        if(newx==x2&&u.y==y2) return u.step+1;
        if(newx>=1&&f[newx][u.y]==0)
        {
            f[newx][u.y]=u.step+1;
            q.push((node){newx,u.y,f[newx][u.y]});
        }
        int newy=u.y+1;
        if(u.x==x2&&newy==y2) return u.step+1;
        if(newy<=n&&f[u.x][newy]==0)
        {
            f[u.x][newy]=u.step+1;
            q.push((node){u.x,newy,f[u.x][newy]});
        }
        newy=u.y-1;
        if(u.x==x2&&newy==y2) return u.step+1;
        if(u.x>=1&&f[u.x][newy]==0)
        {
            f[u.x][newy]=u.step+1;
            q.push((node){u.x,newy,f[u.x][newy]});
        }
    }
}
int main()
{
    int x,y,x2,y2;
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    cin>>x>>y>>x2>>y2;
    cout<<bfs(x,y,x2,y2);
    return 0;
}   

80分,#3,#8wa


by wbw_121124 @ 2023-03-26 17:02:03

……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………


|