RE求调

P1746 离开中山路

Xuwindows_WS @ 2024-08-07 09:47:54

#include<bits/stdc++.h>
using namespace std;
int n;
bool a[1001][1001];
int sx,sy,ex,ey;
int vis[1001][1001];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void bfs()
{
    queue<int>qx,qy;
    int depth=0;
    int ns,nx[10001],ny[10001];
    qx.push(sx);
    qy.push(sy);
    vis[sx][sy]=1;
    while(1)
    {
        if(qx.empty())
        {
            for(int i=1;i<=ns;i++)
            {
                qx.push(nx[i]);
                qy.push(ny[i]);
                nx[i]=0;
                ny[i]=0;
            }
            depth++;
        }
        int x=qx.front();
        int y=qy.front();
        qx.pop();
        qy.pop();
        if(x==ex&&y==ey)
        {
            cout<<depth<<endl;
            exit(0);
        }
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!a[xx][yy]&&!vis[xx][yy])
            {
                vis[xx][yy]=1;
                nx[ns]=xx;
                ny[ns]=yy;
            }
        }
    }
}
int main()
{
    memset(vis,0,sizeof(vis));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            char c;
            cin>>c;
            if(c=='0')a[i][j]=0;
            else a[i][j]=1;
        }
    }
    cin>>sx>>sy>>ex>>ey;
    bfs();
    return 0;
}

by Lovely_puppy @ 2024-08-07 10:02:24

好像是 void bfs ()while(1) 里面


by Xuwindows_WS @ 2024-08-07 10:15:27

但是有退出条件exit(0)


|