【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 ...
随机推荐
- Bootstrap历练实例:模态框(Modal)插件
模态框(Modal)是覆盖在父窗体上的子窗体.通常,其目的是显示来自一个单独源的内容,可以在不离开父窗体的情况下进行一些交互,子窗体提供一些交互或信息. <!DOCTYPE html>&l ...
- 类库日期和jsp导包
一.日期类库 1.1. Date Date类创建一个时间,或者是创建一个与你计算机当前的时间:精确到毫秒. //实例化时间类 Date date = new Date(); 1.2.格式转换类 1.2 ...
- 代码块(block)的使用
Objective-C语法之代码块(block)的使用 代码块本质上是和其他变量类似.不同的是,代码块存储的数据是一个函数体.使用代码块是,你可以像调用其他标准函数一样,传入参数数,并得到返回值. 脱 ...
- retain, copy, assign以及autorelease
一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...
- JavaScript递归实现对象深拷贝
let personOne = { name:"张三", age:18, sex:"male", children:{ first:{ name:"z ...
- pandas处理较大数据量级的方法 - chunk,hdf,pkl
前情提要: 工作原因需要处理一批约30G左右的CSV数据,数据量级不需要hadoop的使用,同时由于办公的本本内存较低的缘故,需要解读取数据时内存不足的原因. 操作流程: 方法与方式:首先是读取数据, ...
- LeetCode(287)Find the Duplicate Number
题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- German Collegiate Programming Contest 2018 B. Battle Royale
Battle Royale games are the current trend in video games and Gamers Concealed Punching Circles (GCPC ...
- Linux学习-备份要点
备份资料的考虑 老实说,备份是系统损毁时等待救援的救星!因为你需要重新安装系统时, 备份的好坏会影响到你 系统复原的进度!事实上,系统有可能由于不预期的伤害而导致系统发生错误! 什么是不预期的伤害呢? ...
- selenium2自动处理验证码
最近在研究web自动化,登录时发现要输入验证码,之前在做手机app自动化时,就被验证码block了.这次做web时又遇到了,探索之后,发现有如下几个解决办法: 1.联系开发人员,让其帮忙在测试环境中注 ...