#include <bits/stdc++.h> // By Lucky Ox
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long
#define endl '\n'
#define pii pair<int, int>
const int N = 3e2 + 10;
int n, m;
char g[N][N];
bool vs[N][N];
map<char, pii[2]> mp;
int dx[] = {0, 1, 0, -1, 0}, dy[] = {0, 0, 1, 0, -1};
int bfs(int fx, int fy){
queue<pair<pii, int> >q;
q.push({{fx, fy}, 0});
vs[fx][fy] = 1;
while(q.size()){
auto now = q.front();
q.pop();
int x = now.first.first, y = now.first.second, cnt = now.second;
if(g[x][y] == '=') return cnt;
for(int i = 1; i <= 4; i ++ ){
int nx = x + dx[i], ny = y + dy[i];
if(g[nx][ny] != '#' && !vs[nx][ny]){
vs[nx][ny] = 1;
if(g[nx][ny] >= 'A' && g[nx][ny] <= 'Z'){
char op = g[nx][ny];
if(mp[op][0].first == nx && mp[op][0].second == ny)
nx = mp[op][1].first, ny = mp[op][1].second;
else
nx = mp[op][0].first, ny = mp[op][0].second;
}
q.push({{nx, ny}, cnt + 1});
}
}
}
}
signed main(){
IOS;
cin >> n >> m;
int fx = 0, fy = 0;
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ ){
cin >> g[i][j];
if(g[i][j] == '@') fx = i, fy = j;
else if(g[i][j] >= 'A' && g[i][j] <= 'Z'){
if (mp[g[i][j]][0].first == 0)
mp[g[i][j]][0].first = i, mp[g[i][j]][0].second = j;
else
mp[g[i][j]][1].first = i, mp[g[i][j]][1].second = j;
}
}
cout << bfs(fx, fy);
return 0;
}
代码如上 我感觉是map那里出问题了 但我看不出来 太弱了 orz orz