【CodeVS 3290】【NOIP 2013】华容道

时间:2022-04-25 16:19:51

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【CodeVS 3290】【NOIP 2013】华容道

话说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】华容道

【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 &lbrack; NOIP 2013 &rsqb; 华容道 —— bfs &plus; 最短路

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

  4. codevs 3290 华容道(SPFA&plus;bfs)

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

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

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

  6. &lbrack;Noip 2013 Day1-3&rsqb; 货车运输 做法总结

    [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. CentOS7 编译安装 Mongodb &lpar;实测 笔记 Centos 7&period;0 &plus; Mongodb 2&period;6&period;6&rpar;

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...

  2. node平台截取图片模块——jimp

    前几天介绍了一个简单的截图模块——iamges,虽然简单,但是功能还是有很多局限的地方. jimp的优势:1.简单,2.支持回调方式和ES6(promise)语法也可以链式调用 3. 丰富的api   ...

  3. dpkg ---- apt-get ------ aptitude 三种方式的区别 及命令格式

    转自:http://blog.csdn.net/xiaoyanghuaban/article/details/22946987 dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的 ...

  4. stl之deque双端队列容器

    deque与vector很相似,不仅能够在尾部插入和删除元素,还能够在头部插入和删除. 只是当考虑到容器元素的内存分配策略和操作性能时.deque相对vector较为有优势. 头文件 #include ...

  5. Duanxx的STM32学习: 启动模式,BOOT0和BOOT1具体解释

    在画STM32的电路图的时候,关于STM32的启动方式纠结了一下,现有的參考设计都是在STM32的启动选择引脚BOOT0和BOOT1上使用了跳帽,用以人工选择STM32的启动方式,可是在实际应用中这样 ...

  6. 自定义MVP &period;net框架

      一个自定义MVP .net框架 AngleFrame   摘要:本篇是本人在完成.net平台下一个项目时,对于MVP框架引发的一些思考,以及开发了一个小型的配置型框架,名字叫作AngleFrame ...

  7. Linux下查找大文件以及目录

    转自:http://www.cnblogs.com/kerrycode/p/4391859.html 在Windows系统中,我们可以使用TreeSize工具查找一些大文件或文件夹,非常的方便高效,在 ...

  8. 1&period; VIM 系列 - 简单入门,拾起兴趣

    目录 1. 认识模式 1.1 正常模式 1.2 插入模式 1.3 命令模式 1.4 可视模式 2. 常用快捷键 1. 认识模式 vim 一共有四种模: 1. 正常模式 2. 插入模式 3. 命令模式 ...

  9. 使用Python访问微信

    itchat是一个开源的微信个人号接口,使用它我们可以很方便的访问我们个人微信号里的信息.itchat的github地址如下: https://github.com/littlecodersh/itc ...

  10. TimingTool - The Timing Diagram Editor

    TimingTool - The Timing Diagram TimingTool is designed to give electronics engineers an easy to use ...