【CF676D】Theseus and labyrinth(BFS,最短路)
题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门。每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等。现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间。
- «+» means this block has 4 doors (one door to each neighbouring block);
- «-» means this block has 2 doors — to the left and to the right neighbours;
- «|» means this block has 2 doors — to the top and to the bottom neighbours;
- «^» means this block has 1 door — to the top neighbour;
- «>» means this block has 1 door — to the right neighbour;
- «<» means this block has 1 door — to the left neighbour;
- «v» means this block has 1 door — to the bottom neighbour;
- «L» means this block has 3 doors — to all neighbours except left one;
- «R» means this block has 3 doors — to all neighbours except right one;
- «U» means this block has 3 doors — to all neighbours except top one;
- «D» means this block has 3 doors — to all neighbours except bottom one;
- «*» means this block is a wall and has no doors.
思路:预处理方格间的联通情况,随后BFS。注意起点与终点相同的情况ANS=0。
const dx:array[..]of longint=(-,,,);
dy:array[..]of longint=(,-,,);
var map:array[..,..]of char;
t:array[..,..,..]of longint;
b:array[..,..,..]of longint;
d:array[..]of record
x,y,z,s:longint;
end;
a:array[..,..,..,..]of longint;
n,m,i,j,k,p,sx,sy,ex,ey,ans,t1,t2,t3,t4,x,y:longint;
ch:ansistring; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure bfs;
var t,w,x1,y1,ux,uy,uz,i,j,k:longint;
begin
for i:= to n do
for j:= to m do
for k:= to do b[i,j,k]:=-;
t:=; w:=; d[].x:=sx; d[].y:=sy; d[].z:=; d[].s:=;
b[sx,sy,]:=;
repeat
inc(t);
ux:=d[t].x; uy:=d[t].y; uz:=d[t].z;
for i:= to do
begin
x1:=ux+dx[i]; y1:=uy+dy[i];
if (x1>)and(x1<=n)and(y1>)and(y1<=m)and
(b[x1,y1,uz]=-)and(a[uz,ux,uy,i]=) then
begin
inc(w); d[w].x:=x1; d[w].y:=y1; d[w].z:=uz; d[w].s:=d[t].s+;
b[x1,y1,uz]:=d[w].s;
if (x1=ex)and(y1=ey) then
begin
ans:=d[w].s; t:=w+;
end;
end;
end;
if b[ux,uy,uz mod +]=- then
begin
inc(w); d[w].x:=ux; d[w].y:=uy; d[w].z:=uz mod +; d[w].s:=d[t].s+;
b[ux,uy,uz mod +]:=d[w].s;
end;
until t>=w;
end; begin
//assign(input,'1.in'); reset(input);
//assign(output,'1.out'); rewrite(output);
readln(n,m);
for i:= to n do
begin
readln(ch);
for j:= to m do map[i,j]:=ch[j];
end;
for p:= to do
begin
if p= then
for i:= to n do
for j:= to m do
begin
if map[i,j]='+' then begin t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='-' then begin t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='|' then begin t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='^' then t[i,j,]:=;
if map[i,j]='>' then t[i,j,]:=;
if map[i,j]='<' then t[i,j,]:=;
if map[i,j]='v' then t[i,j,]:=;
if map[i,j]='L' then begin t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='R' then begin t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='U' then begin t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; end;
if map[i,j]='D' then begin t[i,j,]:=; t[i,j,]:=; t[i,j,]:=; end;
end
else
for i:= to n do
for j:= to m do
begin
t1:=t[i,j,]; t2:=t[i,j,]; t3:=t[i,j,]; t4:=t[i,j,];
t[i,j,]:=t1; t[i,j,]:=t3; t[i,j,]:=t4; t[i,j,]:=t2;
end;
for i:= to n do
for j:= to m do
for k:= to do
begin
x:=i+dx[k]; y:=j+dy[k];
if (x<)or(x>n)or(y<)or(y>m) then continue;
if t[i,j,k]+t[x,y,-k]= then a[p,i,j,k]:=;
end;
end; readln(sx,sy);
readln(ex,ey); if (sx=ex)and(sy=ey) then begin writeln(); exit; end;
ans:=-;
bfs;
writeln(ans); // close(input);
// close(output);
end.
【CF676D】Theseus and labyrinth(BFS,最短路)的更多相关文章
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
- codeforces 676D Theseus and labyrinth BFS搜索
分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了 #include <cstdio> #include <iost ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- 【25.93%】【676D】Theseus and labyrinth
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- JDBC-防止SQL注入问题
String sql = "select * from user where name = '" + name + "' and password = '" ...
- Jquery之 Ajax /json
前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...
- Java-JFrame窗体美化
Java-JFrame窗体美化 JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化: JFrame美化的大致思路:先将JFrame去除默认美化效果, ...
- JS处理数据四舍五入,tofixed与round的区别
此区别是在做微信端有关绑定设备数据曲线平滑处理的过程中,进行验证时候无意发现. 1 .tofixed方法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.例如将数据Num保留 ...
- python 类的使用
目录 类的继承 类的派生 类的组合 菱形继承问题 多态与多态性 dataclass的使用 类的继承 什么是继承,在生活中,子承父业,父亲和儿子就是继承的关系 在python中,父类和子类(派生类),父 ...
- Applied Nonparametric Statistics-lec1
参考网址: https://onlinecourses.science.psu.edu/stat464/node/2 Binomial Distribution Normal Distribution ...
- cf 1017C
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
原 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件 原文章链接:https://blog.csdn.net/beauxie/article/de ...
- MyBatis多个接口参数报错:Available parameters are [0, 1, param1, param2], 及解决方法
1. sql语句如下: SELECT * FROM tb_crm_user WHERE id = #{userId, jdbcType=INTEGER} AND user_name = #{userN ...
- [python IO学习篇] 补充中文编码
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a7 ...