遇到楼梯特殊处理
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
int x, y, t;
node() {}
node(int xx, int yy, int tt):x(xx), y(yy), t(tt) {}
friend bool operator<(const node &a, const node &b) {
return a.t > b.t;
}
};
char mp[50][50];
bool vis[50][50];
int nx[] = {-1, 0, 1, 0};
int ny[] = {0, 1, 0, -1};
int bfs(int m, int n, int sx, int sy) {
memset(vis, false, sizeof(vis));
priority_queue<node> q;
vis[sx][sy] = true;
q.push(node(sx, sy, 0));
while (!q.empty()) {
node t = q.top();
q.pop();
if (mp[t.x][t.y] == 'T')
return t.t;
for (int i = 0; i < 4; i++) {
int tx = t.x + nx[i];
int ty = t.y + ny[i];
int tt = t.t + 1;
if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*')
continue;
if(mp[tx][ty] == '|' || mp[tx][ty] == '-') {
if (mp[tx][ty] == '|') {
if ((i == 0 || i == 2) && t.t%2 || (i == 1 || i == 3) && t.t%2==0)
tt++;
} else if (mp[tx][ty] == '-') {
if ((i == 0 || i == 2) && t.t%2==0 || (i == 1 || i == 3) && t.t%2)
tt++;
}
tx += nx[i];
ty += ny[i];
if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*')
continue;
}
q.push(node(tx, ty, tt));
vis[tx][ty] = true;
}
}
return -1;
}
int main() {
//freopen("in", "r", stdin);
int m, n;
while(~scanf("%d%d", &m, &n)) {
int sx, sy;
getchar();
for (int i = 1; i <= m; i++)
fgets(mp[i]+1, 50, stdin);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (mp[i][j] == 'S') {
sx = i;
sy = j;
break;
}
printf("%d\n", bfs(m, n, sx, sy));
}
return 0;
}