大模拟+bfs 求助
查看原帖
大模拟+bfs 求助
259625
kk1501201楼主2023/8/20 15:00

RT,全输出不能到达

//洛谷 P4872 OIer们的东方梦 不是车万人谁tm会来硬造这道题啊 
#include<bits/stdc++.h>
using namespace std;
const int N=1145;
struct node
{
	int x,y,time,power; //x,y坐标,time花费时间 power:0无道具;1花妈的花;2妖梦的剑 强度剑(能走一切点)>花(不能走墙 )>空手 
	bool operator < (const node &b)const
	{
		return time>b.time;//小顶堆:使小的处于队首 
	}
};
priority_queue<node>q;
set<pair<int,int> >zi;//紫妈的隙间 
int qx[4]={-1,0,0,1},qy[4]={0,-1,1,0},startx,starty,endx,endy,n,m;
bool vis[N][N][3];//vis[i][j][num]表示在power为num时走过了点(i,j)  坑点:可能有一条路10步到达花费 6秒,另一一条路走9步花费7s,故打访问标记地点应该在出队时 
char mp[N][N];//地图 
//'0'和'M'均为普通道路,'S'为起点'E'为终点 
void walk(node a)
{
	if(vis[a.x][a.y][a.power]==true)return ;
	a.time+1;
	q.push(a);
	return ;
}
void wall(node a)//'1'幻想乡的墙就要遵守符卡规则(雾)没剑你就是过不去 
{
	if(vis[a.x][a.y][a.power]==true)return ;	
	if(a.power==2)
	{
		a.time+1;
		q.push(a);
	}
	return ;
}
void cirno(node a)//'2'小妖怪(baka!)
{
	if(vis[a.x][a.y][a.power]==true)return ;
	if(a.power==0) a.time+=4;
	else a.time++;
	q.push(a);
	return ; 
}  
void daiyousei(node a)//'3'大妖怪(大酱!) 
{
	if(vis[a.x][a.y][a.power]==true)return ;
	if(a.power==0) a.time+=9;
	else a.time++;
	q.push(a);
	return ; 
}
void flower(node a)//'4'太阳花田 幽香:6 
{
	if(a.power==0) a.power=1;
	if(vis[a.x][a.y][a.power]==true)return ;
	a.time++;
	q.push(a);
	return ;
}
void louguan(node a)//'5'这是由妖怪做的剑(ろうかんけん),没有它斩不断的东西! 
{
	a.time++;
	if(vis[a.x][a.y][0]!=true)q.push(a);//不拿剑 
	if(vis[a.x][a.y][2]==true)return ;
	if(a.power!=2)
	{
		a.time+=5;
		a.power=2;
		q.push(a);//拿剑 
	}
	return ;
}
void yukari(node a)//'X'油咖喱老太婆ghfjsagasg的隙间传送门(ps:数据不保证只有0或 2个隙间,就是说可以有很多隙间乱传,每次传送耗时1s且经过当前格子时可以不经过隙间 ) 
{
	a.time++;
	if(vis[a.x][a.y][a.power]!=true)q.push(a);
	a.time++; 
 	for(set<pair<int, int> >::iterator iter=zi.begin();iter!=zi.end();iter++)
 	{
 		int nx=iter->first,ny=iter->second;
		a.x=nx;a.y=ny;
		if(vis[a.x][a.y][a.power]==true)return ;
		q.push(a);
	} 
	return ;
}
void work(int i,int j,node a)
{
	char c=mp[i][j];
	if(c=='0'||c=='M')walk(a);
	else if(c=='1')wall(a);
	else if(c=='2')cirno(a);
	else if(c=='3')daiyousei(a);
	else if(c=='4')flower(a);
	else if(c=='5')louguan(a);
	else if(c=='X')yukari(a);
	return ;
}
void bfs()//终于有个正常点的函数了 
{
	while(!q.empty())
	{
		node k=q.top();q.pop();
		int tx=k.x,ty=k.y,ttime=k.time,tpower=k.power;
		vis[tx][ty][tpower]=true;
		if(tx==endx&&ty==endy)//到终点了 
		{
			cout<<ttime<<endl;
			return ;
		}
		for(int i=0;i<4;i++)
		{
			int nx=tx+qx[i],ny=ty+qy[i];
			k.x=nx;k.y=ny;
			work(nx,ny,k);
		}
	}
	cout<<"We want to live in the TouHou World forever"<<endl;//此生无悔入东方,来世愿生幻想乡
	return ;
} 
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int main()
{
	node a;
	n=read();m=read();	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			mp[i][j]=getchar();
			if(mp[i][j]=='S')
			{
				a.x=i;
				a.y=j;
				a.time=0;
				a.power=0;
				q.push(a);
			}
			if(mp[i][j]=='E')
			{
				endx=i;endy=j;
			}
			if(mp[i][j]=='X') zi.insert(make_pair(i,j));
		} 
	}
	bfs(); 
}
2023/8/20 15:00
加载中...