90分求助
查看原帖
90分求助
1293799
shaoningyuan楼主2024/5/22 16:02

大佬帮我看看为什么最后一个点WA了 此人原代码

#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,ex,ey;
char mp[10005][10005];
bool vis[10005][10005];
int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
bool flag=0;
struct st{
    int x,y,step;
}now,p,pp;
void bfs(int x,int y)
{
    queue<st> que;
    st now={x,y,-1};
    vis[x][y]=1;
    que.push(now);
    while(!que.empty())
    {
        p=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int xx=p.x+dx[i];
            int yy=p.y+dy[i];
            while(xx>=1&&xx<=n&&yy>=1&&yy<=n&&vis[xx][yy]==0&&mp[xx][yy]!='x')
            {
                pp.x=xx; pp.y=yy; pp.step=p.step+1;
                que.push(pp);
                vis[xx][yy]=1;
                if(xx==ex&&yy==ey)
                {
                	flag=1;
                    cout<<pp.step;
                    return ;
                }
                xx=xx+dx[i];
                yy=yy+dy[i];
            }
        }
    }   
}
int  main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='.')
            {
            	vis[i][j]=0;
            }
            else if(mp[i][j]=='x')
            {
            	vis[i][j]=1;
            }
            else if(mp[i][j]=='A')
            {
            	sx=i;
            	sy=j;
            	vis[i][j]=0;
            }
            else if(mp[i][j]=='B')
            {
            	ex=i;
            	ey=j;
            	vis[i][j]=0;
            }
        }
    }
    bfs(sx,sy);
    if(flag==0)
    {
    	cout<<"-1";
    }
    return 0;
}

谢谢各位

2024/5/22 16:02
加载中...