【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 ...
随机推荐
- Eclipse+Tomcat搭建jsp服务器
首先,安装java sdk 环境,这里就不多说了,附上java sdk的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- 经常用到的js函数
//获取样式 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ retu ...
- NOIP2016——一个逗号引发的血案
今年江西省报名人数一下子增起来了 隔壁中学来了80+人(虽然都是来给我们垫底的...临时被老师抓来上战场 总之我们赛区参赛人数总算多起来了(起码没再减50%...连续4年减50%真不是随便说说的... ...
- GTX 750等低配显卡如何玩转Deepfakes?
这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...
- 李涛ps高手之路
下载地址:http://www.ly89.cn/detailR/21.html
- LeetCode(120) Triangle
题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- spring+struts2+hibernate框架依赖pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- German Collegiate Programming Contest 2015
// Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...
- UVa 1452 递推 Jump
约瑟夫变形,先计算出3个数时,最后三个数字的编号. 然后以这三个数为起点,就可以递推出n个数对应的最后三个数字的编号. 递推公式都是一样的. #include <iostream> #in ...
- Hive安装步骤
首先解压压缩包 然后进入bin 执行 ./hive 不过现在hive使用的是自己默认的数据库,不方便,可以通过配置使用MySQL数据库 创建hive-site.xml 粘贴一下内容 <confi ...