跳到正文

题解

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

CLC114514约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年8月16日
收录
2026年8月16日
更新
2026年11月13日
篇幅
约 9 分钟
题目
P14402

版本

  1. v1当前

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

这放模拟赛 T2 有点抽象了吧?

拼尽全力写了一坨 130 行的 IDDFS 进行了滚木次提交拿下了滚木 pts!

思考

题目大意

给定 R×CR \times C 的矩阵,# 表示障碍,.表示空地,在矩阵内部的四周有一圈障碍。

你从 (r1,c1)(r_1,c_1) 出发,向四方向移动,每次移动可以走到此方向上离这个点最远的非障碍物点,并且每次移动都会使本次移动的出发点改变变为障碍。

问到达 (r2,c2)(r_2,c_2) 的最小移动次数。

思路历程

第一眼搜索。

从起点出发,向四方向扩展搜索,遇到 # 就停止。

但无论是 DFS 还是 BFS 都很难解决“起点变障碍”的过程,常数大一点甚至还会 TLE。

怎么办?

解决

正确思路

本题评为紫题的最大难度含量就在于能想到图论建模。

对于任意一个非障碍物的点,我们要分两种情况来讨论:

  • 从这个点出发,向任意一个方向,只需 11 次移动就能到达这个方向上离这个点最远的非障碍物点,我们可以在这两点之间建一条长度为 11 的边;
  • 从这个点出发,向任意一个方向,只需 11 次移动就能到达这个方向上离这个点最远的非障碍物点,因为刚才的起点已经成了障碍物,所以再经过 11 次反方向移动后,只能到达刚才的起点在此方向上的相邻点,我们可以在这两点之间建一条长度为 22 的边。

然后边建好了,只需要在起点和终点之间跑最短路即可,是的,就这么简单。

还有一个重点,要把矩阵中一个二维的坐标 (x,y)(x,y) 转成点的编号,只需要取 C×x+yC \times x + y,就能得到唯一的编号啦。

这题的最短路其实可以用桶优化 dijistra 的,时间复杂度比堆优化省去了一个 log,但是我懒的写。

CODE

AC Code
C++
#include<bits/stdc++.h>
//#include<windows.h>
//#include<mmsystem.h>
#define int long long
#define ULL unsigned long long
#define double long double
#define PII pair<int, int>
#define PQ priority_queue
#define LLinf 0x3f3f3f3f3f3f3f3f
#define eps 1e-9
#define invmod 499122177
using namespace std;
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch > 57 || ch < 48) {
        if(ch == 45) {
        	f = -1;
		}
        ch = getchar();
    }
    while(ch >= 48 && ch <= 57) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
vector<vector<PII> > v(1000005);
char ch[1005][1005];
int dis[1000005];
bool vis[1000005];
int r, c;
int r1, c1, r2, c2;
int dijistra() {
	priority_queue<PII, vector<PII>, greater<PII> > heap;
	memset(dis, 0x3f, sizeof(dis));
	memset(vis, false, sizeof(vis));
    dis[c * r1 + c1] = 0;
    heap.push({0, c * r1 + c1});
    while(! heap.empty()) {
        auto head = heap.top();
        int d = head.first;
        int p = head.second;
        heap.pop();
        if(vis[p]) {
        	continue;
		}
        vis[p] = true;
        if(p == c * r2 + c2) {
        	return d;
		}
        for(auto i : v[p]) {
        	int to = i.first;
        	int len = i.second;
			if(d + len < dis[to]) {
				dis[to] = d + len;
				heap.push({dis[to], to});
			}
        }
    }
    return -1;
}
signed main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	r = read();
	c = read();
	for(int i = 1; i <= r; i ++) {
		for(int j = 1; j <= c; j ++) {
			cin >> ch[i][j];
		}
	}
	r1 = read();
	c1 = read();
	r2 = read();
	c2 = read();
	if(r1 == r2 && c1 == c2) {
		cout << 0;
		return 0;
	}
	for(int i = 2; i < r; i ++) {
        for(int j = 2; j < c; j ++) {
            if(ch[i][j] == '.'){
                int x = i, y = j;
                while(x > 1 && ch[x - 1][y] == '.') {
                	x --;
				}
                if(x != i) {
                	v[c * i + j].push_back({c * x + y, 1});
				}
                x = i, y = j;
                while(x < r && ch[x + 1][y] == '.') {
                	x ++;
				}
                if(x != i) {
                	v[c * i + j].push_back({c * x + y, 1});
				}
                x = i, y = j;
                while(y > 1 && ch[x][y - 1] == '.') {
                    y --;
				}
                if(y != j) {
                    v[c * i + j].push_back({c * x + y, 1});
				}
                x = i, y = j;
                while(y < c && ch[x][y + 1] == '.') {
                	y ++;
				}
                if(y != j) {
                	v[c * i + j].push_back({c * x + y, 1});
				}
            }
        }
    }
    for(int i = 2; i < r; i ++) {
        for(int j = 2; j < c; j ++) {
            if(ch[i][j] == '.') {
                if(i > 1 && ch[i - 1][j] == '.') {
                    v[c * i + j].push_back({c * (i - 1) + j, 2});
                }
                if(i < r && ch[i + 1][j] == '.') {
                    v[c * i + j].push_back({c * (i + 1) + j, 2});
                }
                if(j > 1 && ch[i][j - 1] == '.') {
                    v[c * i + j].push_back({c * i + j - 1, 2});
                }
                if(j < c && ch[i][j + 1] == '.') {
                    v[c * i + j].push_back({c * i + j + 1, 2});
                }
            }
        }
    }
    cout << dijistra();
	return 0;
}
//TODO
//I AKed IOI !!!