#include <bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int n,m;
char maze[305][305],ch;
struct door {
int x1,y1,x2,y2;
}trans[100];
struct point {
int x,y,step;
};
queue<point>q;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};//方向: 上下左右
int main(){
cin >> n >> m;
f(i,1,n) {
f(j,1,m) {
cin >> maze[i][j];
ch=maze[i][j];
//传送装置
if(ch >= 'A' && ch <= 'Z') {
if(trans[ch].x1 == 0) trans[ch].x1 = i,trans[ch].y1 = j;
else trans[ch].x2 = i,trans[ch].y2 = j;
}
if(ch == '@') { //起点
q.push(point{i,j,0});
maze[i][j] = '#';
}
}
} //输入处理完毕
while(!q.empty()) { //bfs搜索
point p = q.front();
q.pop();
f(i,0,3) {
int ux = p.x + dx[i],uy = p.y + dy[i];
if(ux < 1 || ux > n || uy < 1 || uy > m) continue;
ch = maze[ux][uy];
if(ch == '#') continue;
if(ch == '=') {
cout << p.step+1;
return 0;
}
if(ch >= 'A' && ch <= 'Z') {
if(ux == trans[ch].x1) ux = trans[ch].x2,uy = trans[ch].y2;
else ux = trans[ch].x1,uy = trans[ch].y1;
q.push(point{ux,uy,p.step+1});
continue;
}
//下面是迷宫草地
if(ch == '.') {
q.push(point{ux,uy,p.step+1});
//printf("ux %d uy %d step %d............\n",ux,uy,p.step+1);
maze[ux][uy] = '#';
}
}
//printf("p.x=%d p.y=%d p.step=%d\n",p.x,p.y,p.step);
}
return 0;
}
第47行那里本来是 if(ux == trans[ch].x1),这样写就错了2#,3#,4#,三个点,6#TLE了, 改成 if(ux == trans[ch].x1 && uy == trans[ch].y1) 就AC了,
百思不得其解,求大神指点。