救救孩子吧!最后一个测试点死活过不去
查看原帖
救救孩子吧!最后一个测试点死活过不去
1150009
cz_Huang楼主2024/3/19 21:22
#include<iostream>
#include<queue>
#include<stdio.h>
using namespace std;  
static int dx[4]={1,-1,0,0};
static int dy[4]={0,0,1,-1};
typedef struct {
	int x,y;
	long tm; //tm代表到这里的时间 
} index;
int n,m,cvy[30],ifp[400][400]; //ifp判断是否到过  cvy判断传送门是否用过 
long ans=11451400;
char p[400][400];
index covy(int x,int y,char c,long ti)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++) //找到另一个点 
		{
			if(p[i][j]==c&&!(x==i&&y==j))
			return (index){i,j,ti+1};
		}
	}
}
void bfs()
{
	int x1,y1;
	queue <index> q;
	for(int i=0;i<n;i++)  //循环找到起点 
		for(int j=0;j<m;j++)
			if(p[i][j]=='@')
			{
				x1=i;
				y1=j;
			}
	q.push((index){x1,y1,0});
	ifp[x1][y1]=1;
	while(q.size())
	{
		index t=q.front();
		for(int i=0;i<4;i++) //向四个方向遍历 
		{
			int xx=t.x+dx[i],yy=t.y+dy[i];
			if(xx<0||yy<0||xx>=n||yy>=m) continue; //越界了就跳过 
			if(p[xx][yy]=='=')
			{
				ans=t.tm+1;
				return; 
			}
			else if(p[xx][yy]=='.'&&!ifp[xx][yy])
			{
				q.push((index){xx,yy,t.tm+1});
				ifp[xx][yy]=1;
			}
			else if(65<=p[xx][yy]&&p[xx][yy]<=90&&!ifp[xx][yy]) //如果遇到传送门 
			{
				index tem=covy(xx,yy,p[xx][yy],t.tm);
				if(cvy[p[xx][yy]-'A']==0) //如果这个传送门没有用过 
				{
					q.push(tem);
					cvy[p[xx][yy]-'A']=1; //将其置为用过 
				}
				else
				{
					int xt=tem.x,yt=tem.y;
					q.push(tem);
					ifp[xx][yy]=1; //第二次用传送门后将二者都置为用过 
					ifp[xt][yt]=1;
				}
			}
		}
		q.pop();		
	}
}
int main(void)
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>p[i][j];	
	bfs();
	cout<<ans;
}
2024/3/19 21:22
加载中...