Luogu1979 NOIP2013D2T3 华容道 搜索、最短路
题意:给出一个$N \times M$的棋盘,棋盘上有一些块可以移动,有一些块无法移动。$Q$次询问,每一次询问给出三个块$a,b,c$,将$a$块变为空格,空格旁边可移动的块可以与空格交换位置。问每一次询问中最小的将$b$块移动到$c$块最开始位置上的移动次数。$N , M \leq 30 , Q \leq 500$
我觉得我在$NOIP$考场上绝对会直接打暴力qwq
我们能够发现空格必须要在需要移动的格子的四周,而且不移动需要移动的格子,才能发挥效果。所以当空格在需要移动的格子旁边的时候,只有两种情况:①将需要移动的格子与空格交换位置;②将空格移动到需要移动的格子的另一侧。所以我们预处理:$f_{i,j,k,l}$表示将空格从格子$i,j$的方向$k$移动到方向$l$且不移动$(i,j)$的最少步数,可以通过$bfs$实现,复杂度$O(16N^2M^2)$
接下来就是一个类似于最短路的问题了。然而最开始空格与需要移动的格子不相邻,所以我们在每一次询问的时候,再一次$bfs$计算现在空格的位置到达需要移动的格子四周且不移动需要移动的格子的最少移动次数,然后跑$SPFA$即可。因为图很小,卡不了$SPFA$。
#include<bits/stdc++.h> using namespace std; inline int read(){ ; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)){ a = (a << ) + (a << ) + (c ^ '); c = getchar(); } return a; } ][] = {,,,,-,,,-}; ][][][] , dis[][][] , t[][] , N , M , Q; ][] , inq[][][]; struct be{ int x , y , dir; }now; queue < pair < int , int > > q; queue < be > q1; inline int SPFA(int aX , int aY , int bX , int bY , int cX , int cY){ while(!q.empty()) q.pop(); if(!canbe[aX][aY] || !canbe[bX][bY]) return 0x3f3f3f3f; memset(t , 0x3f , sizeof(t)); t[aX][aY] = ; q.push(make_pair(aX , aY)); while(!q.empty()){ pair < int , int > r = q.front(); q.pop(); if(r.first == bX && r.second == bY) return t[bX][bY]; ; i < ; i++) ] != cX || r.second + dir[i][] != cY) ]][r.second + dir[i][]]) ]][r.second + dir[i][]] > t[r.first][r.second] + ){ t[r.first + dir[i][]][r.second + dir[i][]] = t[r.first][r.second] + ; q.push(make_pair(r.first + dir[i][] , r.second + dir[i][])); } } return 0x3f3f3f3f; } inline void bfs(int sX , int sY , int tX , int tY){ ; i < ; i++) if(dis[sX][sY][i] != 0x3f3f3f3f){ inq[sX][sY][i] = ; q1.push((be){sX , sY , i}); } while(!q1.empty()){ now = q1.front(); inq[now.x][now.y][now.dir] = ; q1.pop(); if(now.x == tX && now.y == tY) continue; ; i < ; i++) if(now.dir != i){ int N = dis[now.x][now.y][now.dir] + f[now.x][now.y][now.dir][i]; if(dis[now.x][now.y][i] > N){ dis[now.x][now.y][i] = N; if(!inq[now.x][now.y][i]){ inq[now.x][now.y][i] = ; q1.push((be){now.x , now.y , i}); } } } ]][now.y + dir[now.dir][]][ - now.dir] > dis[now.x][now.y][now.dir] + ){ dis[now.x + dir[now.dir][]][now.y + dir[now.dir][]][ - now.dir] = dis[now.x][now.y][now.dir] + ; ]][now.y + dir[now.dir][]][ - now.dir]){ inq[now.x + dir[now.dir][]][now.y + dir[now.dir][]][ - now.dir] = ; q1.push((be){now.x + dir[now.dir][] , now.y + dir[now.dir][] , - now.dir}); } } } } int main(){ N = read(); M = read(); Q = read(); ; i <= N ; i++) ; j <= M ; j++) canbe[i][j] = read(); memset(f , 0x3f , sizeof(f)); ; i <= N ; i++) ; j <= M ; j++) if(canbe[i][j]) ; m <= ; m++) ; n <= ; n++) f[i][j][m][n] = SPFA(i + dir[m][] , j + dir[m][] , i + dir[n][] , j + dir[n][] , i , j); while(Q--){ int a = read() , b = read() , c = read() , d = read() , e = read() , f = read(); if(c == e && d == f){ printf("0\n"); continue; } memset(dis , 0x3f , sizeof(dis)); ; i < ; i++) dis[c][d][i] = SPFA(a , b , c + dir[i][] , d + dir[i][] , c , d); bfs(c , d , e , f); int ans = 0x3f3f3f3f; ; i < ; i++) ans = min(ans , dis[e][f][i]); printf( : ans); } ; }
Luogu1979 NOIP2013D2T3 华容道 搜索、最短路的更多相关文章
- NOIp2013D2T3 华容道【搜索&图论-最短路】
题目传送门 暴力搜索 看到这道题的第一反应就是直接上$bfs$啦,也没有想到什么更加优秀的算法. 然后就是$15$分钟打了$70$分,有点震惊,纯暴力诶,这么多白给分嘛,太划算了,这可是$D2T3$诶 ...
- Remmarguts' Date POJ - 2449 (A*搜索|k短路)
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- 2018/7/16 YMOI模拟 NOIP2013D2T3华容道
题目描述 Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. ...
- [題解]TYVJ_2032(搜索/最短路)
搜索:https://www.cnblogs.com/SiriusRen/p/6532506.html?tdsourcetag=s_pctim_aiomsg 來自 SiriusRen 數據範圍小,考慮 ...
- NOIP 2017 逛公园 记忆化搜索 最短路 好题
题目描述: 策策同学特别喜欢逛公园.公园可以看成一张N个点MM条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要花的时间. ...
- Luogu P1979 华容道(bfs+最短路)
P1979 华容道 题意 题目描述 小B最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成, 最少需要多少时间. ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- LOJ2613 NOIP2013 华容道 【最短路】*
LOJ2613 NOIP2013 华容道 LINK 这是个好题,具体题意比较麻烦可以直接看LINK中的链接 然后考虑我们可能的移动方式 首先我们需要把白块移动到需要移动块S的附近(附近四格) 然后我们 ...
随机推荐
- SolrCloud集群搭建(基于zookeeper)
1. 环境准备 1.1 三台Linux机器,x64系统 1.2 jdk1.8 1.3 Solr5.5 2. 安装zookeeper集群 2.1 分别在三台机器上创建目录 mkdir /usr/hdp/ ...
- Python之逻辑回归
代码: import numpy as np from sklearn import datasets from sklearn.linear_model import LogisticRegress ...
- 如何将 asp.net core 应用进行 docker 容器部署
asp.net core 部署在 docker 容器中比较简单,但常因asp.net core程序发布的问题造成容器无法正常启动.现在把详细的操作的步骤记录如下: 一.asp.net core web ...
- MSSQL一种取代游标的方案
今天看到一篇文章写的自己整理记录下,据说比用游标快. DECLARE @字段1 数据类型; DECLARE @字段2 数据类型; DECLARE @TMP_WHILE_ID INT; ,),TMP_W ...
- CSS| 框模型-margin
CSS margin 属性 设置外边距的最简单的方法就是使用 margin 属性. margin 属性接受任何长度单位,可以是像素.英寸.毫米或 em. margin 可以设置为 auto.更常见的做 ...
- Vim命令图解及快捷键讲解
快捷键详解
- Java中常用的字节流和字符流
IO流(输入流.输出流) 字节流.字符流 1.字节流: InputStream.OutputStream InputStream抽象了应用程序读取数据的方式: OutputStream抽象了应用程序写 ...
- January 21st, 2018 Week 3rd Sunday
Your attitude, not your aptitude, will determine your altitude. 决定你人生高度的,不是你的才能,而是你的态度. Actually, at ...
- MATLAB数值分析实验
1.用Newton迭代法求方程 的第一个正根. 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ newton.m: function x1=newto ...
- Nginx主程序使用介绍
守护进程和服务 <br\>在首次运行Nginx之前,了解此应用程序的性质很重要. 有两种类型的计算机应用程序 – 那些需要用户输入,因此在前台运行,另一种在后台运行. Nginx是后一种类 ...