#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cctype>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;
int n,m;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
char a[305][305], cnt;
struct hyb
{
int x,y,step;
};
struct hyb2
{
int x1,y1,x2,y2;
}cs[105];
int main()
{
cin>>n>>m;
queue<hyb>q;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
cnt=a[i][j];
if(cnt>='A'&&cnt<='Z')
{
if(cs[cnt].x1==0)
{
cs[cnt].x1=i;
cs[cnt].y1=j;
}
else
{
cs[cnt].x2=i;
cs[cnt].y2=j;
}
}
if(cnt=='@')
{
hyb tp={i,j,0};
q.push(tp);
a[i][j]='#';
}
}
}
while(q.empty()==false)
{
hyb tp=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=tp.y+dx[i];
int yy=tp.y+dy[i];
if(xx<1||xx>n||yy<1||yy>m)
{
continue;
}
if(a[xx][yy]=='#')
{
continue;
}
if(a[xx][yy]=='.')
{
hyb t={xx,yy,tp.step+1};
q.push(t);
a[xx][yy]='#';
continue;
}
if(a[xx][yy]=='=')
{
cout<<tp.step+1;
return 0;
}
if(cs[a[xx][yy]].x1==xx&&cs[a[xx][yy]].y1==yy)
{
hyb t={cs[a[xx][yy]].x2,cs[a[xx][yy]].y2,tp.step+1};
q.push(t);
}
else
{
hyb t={cs[a[xx][yy]].x1,cs[a[xx][yy]].y1,tp.step+1};
q.push(t);
}
}
}
return 0;
}
代码求调