bfs 21分求助QWQ(玄关)
查看原帖
bfs 21分求助QWQ(玄关)
1199534
ycy1124楼主2024/4/6 14:32
#include<bits/stdc++.h>
using namespace std;
queue <int> x,y,t1;
int p1[6]={0,1,-1,0,0},p2[6]={0,0,0,1,-1};
int m;
bool bj,pd[301][301],vis[301][301];
int t[301][301],ans=-1;
void bfs(int x1,int y1,int t0)
{
	vis[x1][y1]=1;
	if(pd[x1][y1]==0)
	{
		ans=t0;
		bj=1;
		return;
	}
	for(int i=1;i<=4;i++)
	{
		if((t[abs(x1-p1[i])][abs(y1-p2[i])]==0||t[abs(x1-p1[i])][abs(y1-p2[i])]-t0>=1)&&!vis[abs(x1-p1[i])][abs(y1-p2[i])])
		{
			x.push(abs(x1-p1[i]));
			y.push(abs(y1-p2[i]));
			t1.push(t0+1);
			vis[abs(x1-p1[i])][abs(y1-p2[i])]=1;
		}
	}
}
int main()
{
	int x1,y1,t0;
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>x1>>y1>>t0;
		for(int i=0;i<=4;i++)
		{
			pd[abs(x1-p1[i])][abs(y1-p2[i])]=1;
			if(t[abs(x1-p1[i])][abs(y1-p2[i])]!=0)
			{
				t[abs(x1-p1[i])][abs(y1-p2[i])]=min(t[abs(x1-p1[i])][abs(y1-p2[i])],t0);
			}
			else
			{
				t[abs(x1-p1[i])][abs(y1-p2[i])]=t0;
			}
		}
	}
	x.push(0);
	y.push(0);
	t1.push(0);
	while((!x.empty()||!y.empty())&&!bj)
	{
		bfs(x.front(),y.front(),t1.front());
		x.pop();
		y.pop();
		t1.pop();
	}
	cout<<ans;
	return 0;
}

2024/4/6 14:32
加载中...