这道题第一眼看是暴力,然后发现直接暴力会TLE。

把问题转换一下:移动空格到处跑,如果空格跑到指定位置的棋子,交换位置。

这个可以设计一个状态:$[x1][y1][x2][y2]$,表示空格在$(x1,\ y1)$,棋子在$(x2,\ y2)$的状态,可以向四个方向进行转移。

直接转移,对于每一组询问,都要用 $O(n^4)$ 的时间处理,所以复杂度是 $O(n^4 \times q)$。$70pts$。

观察发现有很多状态是无用状态,只有指定棋子的上下左右四个位置的状态有价值。

然后建个图,会发现不需要在询问时处理,跑一遍最短路,即可AC。

复杂度:$O(n^4)\ +\ O(n^2\ logn \times q)$

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; #define re register
#define LL long long
#define rep(i, x, y) for (register int i = x; i <= y; ++i)
#define repd(i, x, y) for (register int i = x; i >= y; --i)
#define maxx(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
#define inf 1e9
#define linf 1e17 inline int read() {
re int w = , f = ; char c = getchar();
while (!isdigit(c)) f = c == '-' ? - : f, c = getchar();
while (isdigit(c)) w = (w << ) + (w << ) + (c ^ ), c = getchar();
return w * f;
} const int maxn = + ; struct State {
int x, y, d;
}; queue<State> q; struct Edge {
int u, v, w, pre;
}; inline int convert(int x, int y, int d) {
return x * + y * + d;
} struct Node {
int u, d;
bool operator < (const Node &rhs) const {
return d > rhs.d;
}
}; priority_queue <Node> Q; struct Graph {
Edge edges[maxn * maxn * maxn];
int G[maxn * maxn * maxn], n, m;
int d[maxn * maxn * maxn], vis[maxn * maxn * maxn];
void init(int n) {
this->n = n;
m = ;
memset(G, , sizeof(G));
}
void AddEdge(int u, int v, int w) {
edges[++m] = (Edge){u, v, w, G[u]};
G[u] = m;
}
void dijkstra() {
memset(vis, , sizeof(vis));
while (!Q.empty()) {
re Node head = Q.top(); Q.pop();
int u = head.u;
if (vis[u]) continue;
vis[u] = ;
for (re int i = G[u]; i; i = edges[i].pre) {
Edge &e = edges[i];
if (!vis[e.v] && d[u] + e.w < d[e.v]) {
d[e.v] = d[u] + e.w;
Q.push((Node){e.v, d[e.v]});
}
}
}
}
} G; int fx[] = {, , , -};
int fy[] = {, , -, }; int n, m, t;
int a[maxn][maxn], vis[maxn][maxn], dis[maxn][maxn]; void bfs(int X, int Y) {
memset(dis, 0x3f, sizeof(dis));
dis[X][Y] = ;
q.push((State){X, Y, });
while (!q.empty()) {
State head = q.front(); q.pop();
dis[head.x][head.y] = head.d;
rep(i, , ) {
re int x = head.x + fx[i], y = head.y + fy[i];
if (a[x][y] && !vis[x][y]) {
vis[x][y] = ;
dis[x][y] = head.d + ;
q.push((State){x, y, dis[x][y]});
}
}
}
} int main() {
n = read(), m = read(), t = read(); rep(i, , n)
rep(j, , m)
a[i][j] = read(); G.init(n * n * ); rep(X, , n)
rep(Y, , m)
if (a[X][Y]) {
rep(i, , ) {
re int x = X + fx[i], y = Y + fy[i];
if (a[x][y]) {
memset(vis, , sizeof(vis));
vis[x][y] = vis[X][Y] = ;
bfs(x, y);
rep(j, , ) {
re int x2 = X + fx[j], y2 = Y + fy[j];
if (i == j || !a[x2][y2]) continue;
G.AddEdge(convert(X, Y, i), convert(X, Y, j), dis[x2][y2]);
}
}
}
if (a[X][Y + ]) G.AddEdge(convert(X, Y, ), convert(X, Y + , ), );
if (a[X + ][Y]) G.AddEdge(convert(X, Y, ), convert(X + , Y, ), );
if (a[X][Y - ]) G.AddEdge(convert(X, Y, ), convert(X, Y - , ), );
if (a[X - ][Y]) G.AddEdge(convert(X, Y, ), convert(X - , Y, ), );
} re int Ex, Ey, Sx, Sy, Tx, Ty; while (t--) {
Ex = read(), Ey = read(), Sx = read(), Sy = read(), Tx = read(), Ty = read(); if (Sx == Tx && Sy == Ty) {
printf("0\n");
continue;
} memset(vis, , sizeof(vis));
vis[Ex][Ey] = vis[Sx][Sy] = ;
bfs(Ex, Ey);
memset(G.d, 0x3f, sizeof(G.d));
rep(i, , ) {
Q.push((Node){convert(Sx, Sy, i), dis[Sx + fx[i]][Sy + fy[i]]});
G.d[convert(Sx, Sy, i)] = dis[Sx + fx[i]][Sy + fy[i]];
}
G.dijkstra(); re int ans = inf;
rep(i, , )
if (a[Tx + fx[i]][Ty + fy[i]]) minn(ans, G.d[convert(Tx, Ty, i)]);
if (ans == inf) printf("-1\n");
else printf("%d\n", ans);
} return ;
}

[NOIP2013提高组]华容道的更多相关文章

  1. [NOIP2013 提高组] 华容道 P1979 洛谷

    [NOIP2013 提高组] 华容道 P1979 洛谷 强烈推荐,更好的阅读体验 经典题目:spfa+bfs+转化 题目大意: 给出一个01网格图,和点坐标x,y空格坐标a,b,目标位置tx,ty要求 ...

  2. [NOIp2013提高组]积木大赛/[NOIp2018提高组]铺设道路

    [NOIp2013提高组]积木大赛/[NOIp2018提高组]铺设道路 题目大意: 对于长度为\(n(n\le10^5)\)的非负数列\(A\),每次可以选取一个区间\(-1\).问将数列清零至少需要 ...

  3. [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)

    一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...

  4. 【NOIP2013提高组T3】加分二叉树

    题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...

  5. NOIP2013 提高组day2 3 华容道 BFS

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

  6. [NOIP2013] 提高组 洛谷P1979 华容道

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

  7. 洛谷P1979 [NOIP2013提高组Day2T3]华容道

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

  8. 3537. 【NOIP2013提高组day2】华容道(搜索 + 剪枝)

    Problem 给出一个类似华容道的图.\(q\)次询问,每次给你起始点,终止点,空格位置,让你求最少步数 \(n,m\le 30, q\le 500\). Soultion 一道智障搜索题. 弱智想 ...

  9. NOIP2013提高组D2T3 华容道

    n<=30 * m<=30 的地图上,0表示墙壁,1表示可以放箱子的空地.q<=500次询问,每次问:当空地上唯一没有放箱子的空格子在(ex,ey)时,把位于(sx,sy)的箱子移动 ...

随机推荐

  1. 使用git管理github上的代码

    第一次接触git是使用git来提交自己的github的代码,在new repository之后,github会给出一些操作示例. 示例如下: …or create a new repository o ...

  2. JAVA设计模式-单例模式(Singleton)线程安全与效率

    一,前言 单例模式详细大家都已经非常熟悉了,在文章单例模式的八种写法比较中,对单例模式的概念以及使用场景都做了很不错的说明.请在阅读本文之前,阅读一下这篇文章,因为本文就是按照这篇文章中的八种单例模式 ...

  3. Transformer各层网络结构详解!面试必备!(附代码实现)

    1. 什么是Transformer <Attention Is All You Need>是一篇Google提出的将Attention思想发挥到极致的论文.这篇论文中提出一个全新的模型,叫 ...

  4. Codeforces Numbers 题解

    这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...

  5. event.stopPropagation()、event.preventDefault()与return false的区别

    做小demo时经常用到return false来取消默认事件,但一直不是很懂它和preventDefault()等的区别,今天查了查文档和大神们的博客,在这里对相关知识点做一个总结 首先开门见山,总结 ...

  6. Oracle 查询真实执行计划

    什么是真实执行计划 获取Oracle的执行计划,有几种方式.(本文使用Oracle 11g XE版本,以及普通用户scott登录) explain plan for 有两个步骤: explain pl ...

  7. if [ $# -ne 1 ] 作用

    在shell脚本中经常会使用if [ $# -ne 1 ];then...这类脚本 ];then 这段命令是用于判断参数的个数是否为1,不是则进行then的逻辑处理,其中$#表示参数个数,-ne是不等 ...

  8. 蓝松短视频SDK支持AE模板, 可做类似微商视频, 小柿饼的效果等

    AE模板: 是指设计师用Adobe After Effect做好各种视频动画,比如炫酷视频,文艺/搞笑的场景,相册效果等,根据我们的指导文件导出.蓝松SDK会解析导出的文件,自动还原成AE设计时的动画 ...

  9. Google资深工程师深度讲解Go语言★

    课程目录 第1章 课程介绍 第2章 基础语法 第3章 内建容器 第4章 面向“对象” 第5章 面向接口 第6章 函数式编程 第7章 错误处理和资源管理 第8章 测试与性能调优 第9章 Goroutin ...

  10. Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用

    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...