题解:P14402 [JOISC 2016] 危险的滑冰 / Dangerous Skating

· · 题解

题目概括:

P14402 [JOISC 2016] 危险的滑冰 / Dangerous Skating

给出一个 R\times C 的网格和起止点,网络中有若干障碍物,每次移动原地生成一个障碍物,求最少要多少步才能到达终点,若无法到达输出 -1。

思路分析:

  1. 我们考虑到这是一道比较像最短路的题目,难点在于动态变化的障碍物,所以考虑如何转化为静态或者消除。
  2. 我们考虑建模时有直接到达和间接到达两种可能,我们分别考虑:
    • 直接到达:可以存储每行每列的障碍物,然后使用 lower_bound 寻找上下左右的障碍物,复杂度为 O(RC(\log R\log C))。比较够用。
    • 间接到达:我们注意到一个格子可以用很简单的方式到达相邻格子,因为我们可以向我们想要去的方向滑行在当前位置生成冰块,然后再朝着相反方向滑回来,这样就能到达相邻的一个格子,共需要两步操作,由于格子可以两两到达所以我们就建模完毕了。
  3. 对于间接到达很显然我们会动态产生冰块干涉运动,但仔细考虑不难发现生成的冰块一定覆盖在路径内部,如果之后的某一步我们期望到达的格子被之前的操作放置了冰块,那么这说明我们之前到达过这里,也就是我们绕了一大圈,具体的这一定形成了一个环所以答案一定更劣,故我们不需要考虑这种情况。
  4. 做好建模之后直接存图,然后跑一下单源最短路就可以了。

具体实现流程:

  1. 随机的缺省源。
    #include <bits/stdc++.h>
    using namespace std;
    struct node {
     int v, w;
    };
    int n, m, dis[1000001];
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    bool vis[1000001];
    char mp[1001][1001];
    vector<int> h[1001], l[1001];
    vector<node> e[1000001];
  2. 给对应坐标编号。
    int cala(int x, int y) {
     return (x - 1) * m + y;
    }
  3. dijkstra 模板。
    void dijkstra(int s) {
     memset(dis, 0x3f, sizeof(dis));
     priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
     dis[s] = 0;
     q.push({ 0, s });
     while (!q.empty()) {
         int u = q.top().second;
         q.pop();
         if (vis[u])
             continue;
         vis[u] = true;
         for (int i = 0; i < e[u].size(); i++) {
             int v = e[u][i].v, w = e[u][i].w;
             if (dis[v] > dis[u] + w) {
                 dis[v] = dis[u] + w;
                 q.push({ dis[v], v });
             }
         }
     }
    }
  4. 读入图并找到每行每列的原有冰块。
    int main() {
     cin >> n >> m;
     for (int i = 1; i <= n; i++)
         for (int j = 1; j <= m; j++) {
             cin >> mp[i][j];
             if (mp[i][j] == '#') {
                 h[i].push_back(j);
                 l[j].push_back(i);
             }
         }
  5. 存入可以直接到达的边。
     for (int i = 1; i <= n; i++)
         for (int j = 1; j <= m; j++)
             if (mp[i][j] == '.') {
                 int tj = *lower_bound(h[i].begin(), h[i].end(), j), tj2 = *prev(lower_bound(h[i].begin(), h[i].end(), j));
                 e[cala(i, j)].push_back({ cala(i, tj - 1), 1 });
                 e[cala(i, j)].push_back({ cala(i, tj2 + 1), 1 });
                 int ti = *lower_bound(l[j].begin(), l[j].end(), i), ti2 = *prev(lower_bound(l[j].begin(), l[j].end(), i));
                 e[cala(i, j)].push_back({ cala(ti - 1, j), 1 });
                 e[cala(i, j)].push_back({ cala(ti2 + 1, j), 1 });
  6. 存入相邻格子的边。
                 for (int k = 0; k < 4; k++) {
                     int tx = i + dx[k], ty = j + dy[k];
                     if (mp[tx][ty] == '.')
                         e[cala(i, j)].push_back({ cala(tx, ty), 2 });
                 }
             }
  7. 跑最短路并输出答案。
     int sx, sy, ex, ey;
     cin >> sx >> sy >> ex >> ey;
     dijkstra(cala(sx, sy));
     if (dis[cala(ex, ey)] == 0x3f3f3f3f)
         cout << -1;
     else
         cout << dis[cala(ex, ey)];
     return 0;
    }

    完整代码:

    #include <bits/stdc++.h>
    using namespace std;
    struct node {
     int v, w;
    };
    int n, m, dis[1000001];
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    bool vis[1000001];
    char mp[1001][1001];
    vector<int> h[1001], l[1001];
    vector<node> e[1000001];
    int cala(int x, int y) {
     return (x - 1) * m + y;
    }
    void dijkstra(int s) {
     memset(dis, 0x3f, sizeof(dis));
     priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
     dis[s] = 0;
     q.push({ 0, s });
     while (!q.empty()) {
         int u = q.top().second;
         q.pop();
         if (vis[u])
             continue;
         vis[u] = true;
         for (int i = 0; i < e[u].size(); i++) {
             int v = e[u][i].v, w = e[u][i].w;
             if (dis[v] > dis[u] + w) {
                 dis[v] = dis[u] + w;
                 q.push({ dis[v], v });
             }
         }
     }
    }
    int main() {
     cin >> n >> m;
     for (int i = 1; i <= n; i++)
         for (int j = 1; j <= m; j++) {
             cin >> mp[i][j];
             if (mp[i][j] == '#') {
                 h[i].push_back(j);
                 l[j].push_back(i);
             }
         }
     for (int i = 1; i <= n; i++)
         for (int j = 1; j <= m; j++)
             if (mp[i][j] == '.') {
                 int tj = *lower_bound(h[i].begin(), h[i].end(), j), tj2 = *prev(lower_bound(h[i].begin(), h[i].end(), j));
                 e[cala(i, j)].push_back({ cala(i, tj - 1), 1 });
                 e[cala(i, j)].push_back({ cala(i, tj2 + 1), 1 });
                 int ti = *lower_bound(l[j].begin(), l[j].end(), i), ti2 = *prev(lower_bound(l[j].begin(), l[j].end(), i));
                 e[cala(i, j)].push_back({ cala(ti - 1, j), 1 });
                 e[cala(i, j)].push_back({ cala(ti2 + 1, j), 1 });
                 for (int k = 0; k < 4; k++) {
                     int tx = i + dx[k], ty = j + dy[k];
                     if (mp[tx][ty] == '.')
                         e[cala(i, j)].push_back({ cala(tx, ty), 2 });
                 }
             }
     int sx, sy, ex, ey;
     cin >> sx >> sy >> ex >> ey;
     dijkstra(cala(sx, sy));
     if (dis[cala(ex, ey)] == 0x3f3f3f3f)
         cout << -1;
     else
         cout << dis[cala(ex, ey)];
     return 0;
    }