求助,为什么会TLE
  • 板块P1363 幻象迷宫
  • 楼主lvyou
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/8/23 19:19
  • 上次更新2023/11/4 09:18:37
查看原帖
求助,为什么会TLE
248547
lvyou楼主2021/8/23 19:19

求助 TLE4个点

#include<bits/stdc++.h>
using namespace std;
struct node{
	long long x,y;
}start;
node make_edge(int x,int y)
{
	node a;
	a.x=x;
	a.y=y;
	return a;
}
int n,m;
bool kz[1505][1505];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
node visit[1505][1505];
unordered_map<long long,bool>mp;
node _mod(node a)
{
	return make_edge((a.x%n+n-1)%n+1,(a.y%m+m-1)%m+1);
}
bool check(node x,node y)
{
	if(visit[y.x][y.y].x!=(-1<<31))
	if((x.x!=visit[y.x][y.y].x)||(x.y!=visit[y.x][y.y].y))return 1;
	return 0;
}
long long hg(node x)
{
	return 5000000*(x.x+5000000)+(x.y+5000000);
}
void input()
{
	mp.clear();
	char c;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	{
		visit[i][j]=make_edge(-1<<31,-1<<31);
		cin>>c;
		if(c=='.')
		{
			kz[i][j]=1;
		}
		else if(c=='S')
		{
			start=make_edge(i,j);
			kz[i][j]=1;
		}
		else kz[i][j]=0;
	}
}
void work()
{
	queue<node>q;
	q.push(start);
	mp[hg(start)]=1;
	visit[start.x][start.y]={start.x,start.y};
	while(!q.empty())
	{
		node x=q.front();q.pop();
		//cout<<"x"<<x.x<<" "<<x.y<<endl;
		node a=_mod(x);
		//cout<<"a"<<a.x<<" "<<a.y<<endl;
		if(check(x,a))
		{
			cout<<"Yes"<<endl;
			return;
		}
		for(int i=0;i<4;i++)
		{
			node xx=make_edge(x.x+dx[i],x.y+dy[i]);
			a=_mod(xx);
			if(!mp[hg(xx)]&&kz[a.x][a.y])
			{
				mp[hg(xx)]=1;
				//cout<<mp[xx];
				q.push(xx);
				if(visit[a.x][a.y].x==(-1<<31))
				visit[a.x][a.y]=make_edge(xx.x,xx.y);
			}
		}
	}
	cout<<"No"<<endl;
}
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>n>>m)
	{
		input();
		work();
	}
	return 0;
}

求助,为什么会超时

2021/8/23 19:19
加载中...