26分求条
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5;
void in(int &x){
char c=getchar();
while (c<'0' || c>'9') c=getchar();
for (x=0; c>='0' && c<='9'; c=getchar())
x=x*10+c-'0';
}
char a[1005][1005];
int vis[1005][1005];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;
int rx, ry, ex, ey;
int xx, yy;
void goto_another(int nx,int ny)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]==a[nx][ny]&&(i!=nx||j!=ny))
{
xx=i;
yy=j;
return ;
}
}
}
}
void dfs(int step, int x, int y)
{
if(x == ex && y == ey)
{
cout << step << endl;
exit(0);
}
if(a[x][y] >= 'A' && a[x][y] <= 'Z')
{
goto_another(x, y);
x = xx;
y = yy;
}
//cout << x << y << endl;
for (int i = 0; i < 4; i++)
{
xx = x + dx[i];
yy = y + dy[i];
if(a[xx][yy] != '#' && !vis[xx][yy] && xx >= 1 && xx <= n && yy > 0 && yy <= m)
{
vis[xx][yy] = 1;
dfs(step + 1, xx, yy);
}
}
}
int main(){
in(n);in(m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == '=')
{
ex = i;
ey = j;
}
else if(a[i][j] == '@')
{
rx = i;
ry = j;
}
}
}
//vis[rx][ry] = 1;
dfs(0, rx, ry);
return 0;
}