#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#define INF 0x7fffffff
using namespace std;
int n,m;
char fm[310][310];
bool vis[310][310];
struct pnt{
int x,y;
}snd[30][2];
queue<pnt> q;
int tim[310][310];
int fx[5]={0,1,-1,0,0};
int fy[5]={0,0,0,1,-1};
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>fm[i][j];
if(fm[i][j]>='A' && fm[i][j]<='Z')
{
int c=fm[i][j]-'A';
if(snd[c][0].x)
{
snd[c][1].x=i;
snd[c][1].y=j;
}
else
{
snd[c][0].x=i;
snd[c][0].y=j;
}
}
if(fm[i][j]=='@')
{
q.push((pnt){i,j});
tim[i][j]=0;
vis[i][j]=1;
}
}
}
while(!q.empty())
{
pnt p=q.front();q.pop();
int nx=p.x,ny=p.y;
if(fm[nx][ny]=='=')
{
printf("%d",tim[nx][ny]);
return 0;
}
for(int i=1;i<=4;i++)
{
int tx=nx+fx[i],ty=ny+fy[i];
if(tx<1 || tx>n || ty<1 || ty>m ||
vis[tx][ty] || fm[tx][ty]=='#') continue;
tim[tx][ty]=tim[nx][ny]+1;
vis[tx][ty]=1;
if(fm[tx][ty]>='A' && fm[tx][ty]<='Z')
{
int c=fm[tx][ty]-'A';
int sx=tx,sy=ty;
if(tx==snd[c][1].x && ty==snd[c][1].y)
{
tx=snd[c][0].x,ty=snd[c][0].y;
}
else
{
tx=snd[c][1].x,ty=snd[c][1].y;
}
tim[tx][ty]=tim[sx][sy];
}
q.push((pnt){tx,ty});
}
}
return 0;
}
WA了第 6 个和第 14 个点
第6个点正解为 272 ,输出为 273
求助 qwq