题意

走迷宫,走一步花费1s,走传送花费3s,求活着到达终点的最短时间。

solution

bfs裸题,把起点扔入队列,然后往四个方向移动搜,或者进入传送,注意路径标记和清空。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q;
int sx, sy, tx, ty;
int dis[500][500];
vector<pair<int, int> > e[400][400];
char s[400][400];
queue<int> qx;
queue<int> qy;
int dir[2][4] = {0, 0, 1, -1, 1, -1, 0, 0};

void bfs()
{
    memset(dis, 0x3f, sizeof(dis));
    while (qx.size()) qx.pop(), qy.pop();
    dis[sx][sy] = 0;
    qx.push(sx), qy.push(sy);
    while (qx.size()) {
      int nowx = qx.front(), nowy = qy.front();
      qx.pop(), qy.pop();
      for (int i = 0; i < e[nowx][nowy].size(); i++) {
        if (s[e[nowx][nowy][i].first][e[nowx][nowy][i].second] != '#') {
          if (dis[e[nowx][nowy][i].first][e[nowx][nowy][i].second] >
              dis[nowx][nowy] + 3) {
            dis[e[nowx][nowy][i].first][e[nowx][nowy][i].second] =
                dis[nowx][nowy] + 3;
            qx.push(e[nowx][nowy][i].first), qy.push(e[nowx][nowy][i].second);
          }
        }
      }
      for (int i = 0; i < 4; i++) {
        if (nowx + dir[0][i] <= 0 || nowy + dir[1][i] <= 0 ||
            nowx + dir[0][i] > n || nowy + dir[1][i] > m ||
            s[nowx + dir[0][i]][nowy + dir[1][i]] == '#')
          continue;
        if (dis[nowx + dir[0][i]][nowy + dir[1][i]] > dis[nowx][nowy] + 1) {
          dis[nowx + dir[0][i]][nowy + dir[1][i]] = dis[nowx][nowy] + 1;
          qx.push(nowx + dir[0][i]), qy.push(nowy + dir[1][i]);
        }
      }
    }
}

int main() {
  while (~scanf("%d%d%d", &n, &m, &q)) {
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i] + 1);
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == 'S') sx = i, sy = j;
        if (s[i][j] == 'T') tx = i, ty = j;
      }
    }
    int a, b, c, d;
    for (int i = 0; i < q; i++) {
      scanf("%d%d%d%d", &a, &b, &c, &d);
      a++, b++, c++, d++;
      e[a][b].push_back(make_pair(c, d));
    }
    bfs();
    if (dis[tx][ty] < 0x3f3f3f3f)
      printf("%d\n", dis[tx][ty]);
    else
      printf("-1\n");
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) e[i][j].clear();
  }
  return 0;
}