蒟蒻bfs全wa,自己没找到问题,样例过了
查看原帖
蒟蒻bfs全wa,自己没找到问题,样例过了
209561
FCB_1899楼主2021/2/26 09:46
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
using namespace std;

bool gone[1001][1001]={0};
bool street[1001][1001];

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

struct node
{
	int x,y;
	int step;
};

int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string s;
		cin>>s;
		for(int j=1;j<=n;j++)
			street[i][j]=s[j-1]-'0';
	}
	int sx,ex,sy,ey;
	cin>>sx>>sy>>ex>>ey;
	if(street[sx][sy]==1||street[ex][ey]==1) return 0;
	queue<node> q;
	node u;
	u.x=u.y=1;
	u.step=0;
	q.push(u);
	gone[1][1]=1;
	while(!q.empty())
	{
		node t=q.front();q.pop();
		if(t.x==n&&t.y==n) {cout<<t.step; return 0;}
		for(int i=0;i<4;i++)
		{
			int nx=t.x+dx[i],ny=t.y+dy[i];
			if(gone[nx][ny]==0&&street[nx][ny]==0&&nx>0&&nx<=n&&ny>0&&ny<=n)
			{
				gone[nx][ny]=1;
				node p;
				p.x=nx;p.y=ny;p.step=t.step+1;
				q.push(p);
			}
		}
	}
	return 0;
}
2021/2/26 09:46
加载中...