http://codevs.cn/problem/3290/

据说2013年的noip非常难,但Purpleslz学长还是AK了。能A掉这道题真心orz。

设状态$(i,j,k)$表示目标棋子在$(i,j)$这个位置,空格在紧贴着目标棋子的$k$方向,$0≤k<4$。

因为目标棋子要移动,空格肯定在它旁边。往空格的方向走一步,空格便出现在它另一边。对于这两个状态连边,边权为1。

为了使目标棋子向某一方向移动,需要目标棋子不动,空格从紧贴着目标棋子的某一方向移动到紧贴着目标棋子的另一个方向。对于固定目标棋子位置但空格对于目标棋子的方向不同的状态之间互相连边,边权需要bfs求得。

对于每个询问,给出初始空格的位置,bfs出初始的空格移动到目标棋子旁边四个位置的最短距离,并连边, 边权为最短距离。

最后跑spfa求出到达目标棋子到达终点需要走的最短路。

时间复杂度$O(nm)$,因为边数是nmk级别的,而且k是个常数,所以把k忽略掉233 _(:з」∠)_k都快比nm大了QwQ

话说spfa的复杂度真的是$O(E)$的吗(/"≡ _ ≡)/~┴┴

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 33;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
} struct node {int nxt, to, w;} E[20003];
int Move[33][33][4][4], point[4003], cnt = 0, a[33][33], n, m, qq;
int number[33][33][4], tot = 0; void ins(int u, int v, int w) {E[++cnt] = (node) {point[u], v, w}; point[u] = cnt;} bool can(int x, int y) {
return ((x >= 1) && (x <= n) && (y >= 1) && (y <= m) && (a[x][y] == 1));
} struct Point {
int x, y, d;
Point(int _x = 0, int _y = 0, int _d = 0) : x(_x), y(_y), d(_d) {}
bool operator == (const Point &A) const {
return x == A.x && y == A.y;
}
} q[1003]; int cross(int x, int y, int h, int hh) {
Point s, t;
s = Point(x + dx[h], y + dy[h], 0);
t = Point(x + dx[hh], y + dy[hh], 0);
a[x][y] = 0;
int head = 0, tail = 1;
q[1] = s; a[s.x][s.y] = 1; Point u, v;
while (head != tail) {
u = q[++head];
if (u == t) break;
for(int d = 0; d < 4; ++d)
if (can(u.x + dx[d], u.y + dy[d])) {
v = Point(u.x + dx[d], u.y + dy[d], u.d + 1);
q[++tail] = v;
a[v.x][v.y] = 0;
}
} for(int i = 1; i <= tail; ++i)
a[q[i].x][q[i].y] = 1; a[x][y] = 1;
if (u == t) return u.d;
else return 0x7fffffff;
} void dealwith(int x, int y) {
int ed;
for(int d = 0; d < 4; ++d)
if (can(x + dx[d], y + dy[d]))
for(int dd = d + 1; dd < 4; ++dd)
if (can(x + dx[dd], y + dy[dd])) {
ed = Move[x][y][d][dd] = Move[x][y][dd][d] = cross(x, y, d, dd);
if (ed != 0x7fffffff) {
ins(number[x][y][d], number[x][y][dd], ed);
ins(number[x][y][dd], number[x][y][d], ed);
}
}
} int dis(Point g, Point s, Point t) {
int head = 0, tail = 1;
s.d = 0; q[1] = s; a[s.x][s.y] = 0; a[g.x][g.y] = 0;
Point u, v;
while (head != tail) {
u = q[++head];
if (u == t) break;
for(int d = 0; d < 4; ++d)
if (can(u.x + dx[d], u.y + dy[d])) {
v = Point(u.x + dx[d], u.y + dy[d], u.d + 1);
q[++tail] = v;
a[v.x][v.y] = 0;
}
} a[g.x][g.y] = 1;
for(int i = 1; i <= tail; ++i)
a[q[i].x][q[i].y] = 1;
if (u == t) return u.d;
else return 0x7fffffff;
} queue <int> Q;
int dist[4003], inq[4003]; void spfa(int s) {
memset(dist, 127, sizeof(int) * (tot + 1));
Q.push(s); dist[s] = 0; inq[s] = true;
int u;
while (!Q.empty()) {
u = Q.front(); Q.pop(); inq[u] = false;
for(int i = point[u]; i; i = E[i].nxt)
if (dist[u] + E[i].w < dist[E[i].to]) {
dist[E[i].to] = dist[u] + E[i].w;
if (!inq[E[i].to]) {
Q.push(E[i].to);
inq[E[i].to] = true;
}
}
}
} int main() {
n = in(); m = in(); qq = in();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
a[i][j] = in(); for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if (a[i][j] == 1)
for(int d = 0; d < 4; ++d)
if (can(i + dx[d], j + dy[d]))
number[i][j][d] = ++tot; for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if (a[i][j] == 1) dealwith(i, j); for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if (a[i][j] == 1)
for(int d = 0; d < 4; ++d)
if (can(i + dx[d], j + dy[d]))
ins(number[i][j][d], number[i + dx[d]][j + dy[d]][(d + 2) % 4], 1); ++tot;
Point e, s, t;
int now = cnt, ans;
while (qq--) {
e.x = in(); e.y = in(); s.x = in(); s.y = in(); t.x = in(); t.y = in();
if (s == t) {puts("0"); continue;}
cnt = now; point[tot] = 0;
for(int d = 0; d < 4; ++d)
if (can(s.x + dx[d], s.y + dy[d]))
ins(tot, number[s.x][s.y][d], dis(s, e, Point(s.x + dx[d], s.y + dy[d], 0)));
spfa(tot);
ans = 0x7fffffff;
for(int d = 0; d < 4; ++d)
ans = min(ans, dist[number[t.x][t.y][d]]);
printf("%d\n", ans == 2139062143 ? -1 : ans);
} return 0;
}

终于A掉了。注意特判起点和终点相同

【CodeVS 3290】【NOIP 2013】华容道的更多相关文章

  1. Luogu 1979 NOIP 2013 华容道(搜索,最短路径)

    Luogu 1979 NOIP 2013 华容道(搜索,最短路径) Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面 ...

  2. noip 2013 华容道

    /*双向bfs (得分和单项的一样多....)70*/ #include<iostream> #include<cstdio> #include<cstring> ...

  3. 洛谷 P1979 [ NOIP 2013 ] 华容道 —— bfs + 最短路

    题目:https://www.luogu.org/problemnew/show/P1979 真是一道好题... 首先考虑暴力做法,应该是设 f[i][j][x][y] 记录指定棋子和空格的位置,然后 ...

  4. codevs 3290 华容道(SPFA+bfs)

    codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s  空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...

  5. NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】

    NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...

  6. [Noip 2013 Day1-3] 货车运输 做法总结

    [Noip 2013 Day1-3] 货车运输 做法总结 Online Judge:Luogu-1967 Label:启发式合并,离线,整体二分,按秩合并,倍增,最大生成树 打模拟离线赛时做到,顺便总 ...

  7. Codevs 3289 花匠 2013年NOIP全国联赛提高组

    3289 花匠 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 花匠栋栋种了一排花,每株花都 ...

  8. 【NOIP 2013 DAY2 T3】 华容道(spfa)

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

  9. 【CodeVS 3289】【NOIP 2013】花匠

    http://codevs.cn/problem/3289/ dp转移,树状数组维护前缀max和后缀max进行优化,$O(nlogn)$. #include<cstdio> #includ ...

随机推荐

  1. Java面向对象编程 第二章 第一个Java应用

    2.1创建Java源文件 Java应用由一个或多个扩展名为".java"的文件构成,这些文件被称为Java源文件,从编译的角度,则被称为编译单元. 本章包含两个Java源文件:Do ...

  2. Github 扩展推荐

    前言 github是个知识的宝库,分享一下自己浏览github使用的浏览器扩展. octotree 功能简介:以文件组织的结构方式查看仓库,再也不用一级一级地翻目录啦.下载单个文件 源码:https: ...

  3. ArrayList的线程安全测试

    public class TestThread implements Runnable{ private List list; CountDownLatch cdl; public TestThrea ...

  4. 转:[版本控管]TortoiseSVN 使用,抓出兇手,使用 Blame 查看每一行最後修改的人是誰

    类似的官方的使用手段有: https://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-dug-blame.html from: http:/ ...

  5. History 对象

    History 对象 History 对象包含用户(在浏览器窗口中)访问过的 URL. History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. 注 ...

  6. Visual Studio 2012 cannot identify IHttpActionResult

    使用ASP.NET Web API构造基于restful风格web services,IHttpActionResult是一个很好的http结果返回接口. 然而发现在vs2012开发环境中,Syste ...

  7. Python-01-基础

    一.安装Python 官方下载地址:https://www.python.org/downloads/ Windows可直接下载安装,安装时勾选自动配置环境变量即可. Linux/OS X默认装有Py ...

  8. 代码滑动panorama-即程序中设置SelectedIndex

    我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能.那么是不是就没有办法了呢?其实我们可以通过设置SelectedItemProp ...

  9. webapp:移动端高清、多屏适配方案(zz)

    来源: http://sentsin.com/web/1212.html 移动端高清.多屏适配方案 背景 开发移动端H5页面 面对不同分辨率的手机 面对不同屏幕尺寸的手机 视觉稿 在前端开发之前,视觉 ...

  10. MvvmLight ToolKit 教程

    MvvmLightToolKit MvvmLightToolKit的源代码是托管在CodePlex上的,我们 可以从这里获取最新版本的源代码,不仅源码,版本发布的日志,更改了哪些,修复了哪些,以及一些 ...