#15没有输出
查看原帖
#15没有输出
40849
AibohDij楼主2024/1/20 13:35
#include <bits/stdc++.h>
using namespace std;
struct cor
{
	int x,y,step;
};
int n,m;
int sx,sy,ex,ey;
queue<cor> q;
int tele[305][305][2];
int mp[305][305];
int tmp[35][2];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
bool flag;
bool check(int x,int y)
{
	if(x<1||x>n||y<1||y>m||mp[x][y]==1)
		return false;
	return true;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	{
		cin.get();
		for(int j=1;j<=m;++j)
		{
			char c;
			cin>>c;
			if(c=='#')
				mp[i][j]=1;
			else
				mp[i][j]=0;
			if(c=='@')
			{
				sx=i;
				sy=j;
			}
			else if(c=='=')
			{
				ex=i;
				ey=j;
			}
			else if(c!='#'&&c!='@'&&c!='#'&&c!='.')
			{
				if(!tmp[c-'A'][0]&&!tmp[c-'A'][1])
				{
					tmp[c-'A'][0]=i;
					tmp[c-'A'][1]=j;
				}
				else
				{
					int u=tmp[c-'A'][0],v=tmp[c-'A'][1];
					tele[u][v][0]=i;
					tele[u][v][1]=j;
					tele[i][j][0]=u;
					tele[i][j][1]=v;
					mp[u][v]=-1;
					mp[i][j]=-1;
				}
			}
		}
	}
	q.push({sx,sy,0});
	++mp[sx][sy];
	while(!q.empty())
	{
		cor t=q.front();
		q.pop();
		int x,y;
		int u,v;
		x=t.x;
		y=t.y;
		if(tele[x][y][0]&&tele[x][y][1])
		{
			++mp[tele[x][y][0]][tele[x][y][1]];
			u=tele[x][y][0];
			v=tele[x][y][1];	
			x=u;
			y=v;
		}
		for(int i=0;i<4;++i)
		{
			if(check(x+dx[i],y+dy[i]))
			{
				q.push({x+dx[i],y+dy[i],t.step+1});
				++mp[x+dx[i]][y+dy[i]];
				if(x+dx[i]==ex&&y+dy[i]==ey)
				{
					cout<<t.step+1<<endl;
					flag=true;
				}			
			}
		}	
		if(flag)
			break;	
	}
	return 0;
}
2024/1/20 13:35
加载中...