#include <iostream>
#include <cmath>
#include <queue>
#include <climits>
using namespace std;
struct cs {
int cx, cy;
};
struct node {
int x, y, s;
};
int N, M;
char mp[305][305];
bool vis[305][305];
int minL = INT_MAX;
cs css[2][26];
int dX[4] = {-1, 0, 0, 1};
int dY[4] = {0, -1, 1, 0};
void bfs(int sx, int sy) {
queue<node> q;
node n = {sx, sy, 0};
q.push(n);
while(!q.empty()) {
node tmp = q.front();
q.pop();
int tx = tmp.x;
int ty = tmp.y;
int ts = tmp.s;
//找到对应存储的传送点坐标,并修改当前的tx, ty
if(mp[tx][ty] >= 'A' && mp[tx][ty] <= 'Z') {
int ttx = tx;
int tty = ty;
if(tx == css[0][mp[ttx][ty] - 'A'].cx) {
tx = css[1][mp[ttx][tty] - 'A'].cx;
ty = css[1][mp[ttx][tty] - 'A'].cy;
}
else if(tx == css[1][mp[ttx][tty] - 'A'].cx) {
tx = css[0][mp[ttx][tty] - 'A'].cx;
ty = css[0][mp[ttx][tty] - 'A'].cy;
}
}
//朝四个方向移动
for(int i = 0; i < 4; i++) {
int nx = tx + dX[i];
int ny = ty + dY[i];
if(nx >= 1 && nx <= N && ny >= 1 && ny <= M && mp[nx][ny] != '#' && !vis[nx][ny]) {
if(mp[nx][ny] == '=') {
printf("%d", ts + 1);
return ;
}
else if(mp[nx][ny] == '.') {
vis[nx][ny] = 1;
tmp = {nx, ny, ts + 1};
} else {
tmp = {nx, ny, ts + 1};
}
q.push(tmp);
}
}
}
}
int main() {
int sx, sy;
scanf("%d%d", &N, &M);
getchar();
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= M; j++) {
mp[i][j] = getchar();
if(mp[i][j] == '@') {
sx = i;
sy = j;
} else if(mp[i][j] <= 'Z' && mp[i][j] >= 'A') {//二维数组的第0行和第1行相互为一组传送点
if(css[0][mp[i][j] - 'A'].cx == 0) {
css[0][mp[i][j] - 'A'].cx = i;
css[0][mp[i][j] - 'A'].cy = j;
} else {
css[1][mp[i][j] - 'A'].cx = i;
css[1][mp[i][j] - 'A'].cy = j;
}
}
}
getchar();
}
vis[sx][sy] = 1;
bfs(sx, sy);
return 0;
}
试了很多次,2,3,4,6出不了答案诶