【CodeVS 3290】【NOIP 2013】华容道
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】华容道的更多相关文章
- Luogu 1979 NOIP 2013 华容道(搜索,最短路径)
Luogu 1979 NOIP 2013 华容道(搜索,最短路径) Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面 ...
- noip 2013 华容道
/*双向bfs (得分和单项的一样多....)70*/ #include<iostream> #include<cstdio> #include<cstring> ...
- 洛谷 P1979 [ NOIP 2013 ] 华容道 —— bfs + 最短路
题目:https://www.luogu.org/problemnew/show/P1979 真是一道好题... 首先考虑暴力做法,应该是设 f[i][j][x][y] 记录指定棋子和空格的位置,然后 ...
- codevs 3290 华容道(SPFA+bfs)
codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...
- NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】
NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...
- [Noip 2013 Day1-3] 货车运输 做法总结
[Noip 2013 Day1-3] 货车运输 做法总结 Online Judge:Luogu-1967 Label:启发式合并,离线,整体二分,按秩合并,倍增,最大生成树 打模拟离线赛时做到,顺便总 ...
- Codevs 3289 花匠 2013年NOIP全国联赛提高组
3289 花匠 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 花匠栋栋种了一排花,每株花都 ...
- 【NOIP 2013 DAY2 T3】 华容道(spfa)
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- 【CodeVS 3289】【NOIP 2013】花匠
http://codevs.cn/problem/3289/ dp转移,树状数组维护前缀max和后缀max进行优化,$O(nlogn)$. #include<cstdio> #includ ...
随机推荐
- PHP的文件操作常用函数
PHP文件操作 1 获得文件名:basename - 返回路径中的文件名部分 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名.如果文件名是以 suffix 结束的,那这一部分也会被 ...
- [No00001E]不出国,学口语-出国口语自然好?才怪咧!
- 转: Eclipse自动提示功能
Eclipse的一个重要功能 2011-07-29 10:20:37 标签:java eclipse editor 休闲 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信 ...
- fancybox的配置项
Fancybox的API和配置选项说明 属性名 默认值 简要说明 padding 10 浏览框内边距,和css中的padding一个意思 margin 20 浏览框外边距,和css中的margin一个 ...
- HTML 学习笔记 CSS样式(外边框 外边框合并)
CSS外边距 围绕在元素边框的空白区域就是外边距 设置外边距会在元素外创建额外的空白 设置外边距的最简单的方法就是使用 margin 属性,这个属性接受任何长度单位.百分数值甚至负值. CSS ma ...
- HDU 1166 敌兵布阵
B - 敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- h1/title,b/strong,i/em 的区别
< strong > 表示html页面上的强调(emphasized text), < em > 表示句子中的强调(即强调语义) 1.b和strong的区别 盲人朋友使用阅读设 ...
- AlwaysOn--Backup Preference
AlwaysOn group的一个新特性是允许在secondary replica进行backup,将backup的负载从primary replica上移除去. 并且提供了Backup prefer ...
- Windows 8.1 应用开发 – 触控操作
与WPF相同Windows 8.1应用中也具有高级触控操作(Manipulation),其中包含了三种常见的触屏手势:平移.缩放.旋转,通过以下四种事件可为控件实现各种触控操作:Manipulatio ...
- IE8中给HTML标签负值报错问题
当通过JS给一个HTML标签设置高宽为负值的时候,会爆出一个“参数无效”的错误 chrome下不会报错,但是元素不会做任何关于负值的改变