题意:给定一张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,最短路)的更多相关文章

  1. 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 ...

  2. codeforces 676D Theseus and labyrinth BFS搜索

    分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了 #include <cstdio> #include <iost ...

  3. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth

    题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...

  4. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  6. 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流

    题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...

  7. BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)

    BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...

  8. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  9. 【25.93%】【676D】Theseus and labyrinth

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. Nuget使用备忘

    菜单:工具-库程序包管理器-管理解决方案的NuGet程序包,搜索,下载,安装 或者 工具-库程序包管理器-程序包管理器控制台,输入PM命令,如: install-package log4net 如果不 ...

  2. 01_4_SERVLET声明周期

    01_4_SERVLET声明周期 1. Servlet的生命周期 生命全过程 加载ClassLoader 实例化 new //客户端第一次请求的时候,只new一次 初始化init(ServletCon ...

  3. 关于OnTimer()使用

    OnTimer()其实是用来响应WM_TIMER消息的,其实OnTimer()就是一个回调函数,不过是系统默认的,当用户使用SetTimer()函数设定一个定时器的时候,只要是第三个参数为NULL,则 ...

  4. NOIP模拟赛 篮球比赛1

    篮球比赛1(basketball1.*) Czhou为了提高机房里各种神牛的身体素质,决定在每次训练后举行篮球比赛.为了保持比赛公平,Czhou要将神牛们分成两队.首先神牛们赛前都要排成固定的队伍:然 ...

  5. centos7安装pgsql及操作命令

    1.下载所需要的数据库版本https://yum.postgresql.org/repopackages.php 2.安装数据库版本包 yum install -y https://download. ...

  6. 封装,封装的原理,Property ,setter ,deleter,多态,内置函数 ,__str__ , __del__,反射,动态导入模块

    1,封装 ## 什么是封装 what 对外隐藏内部的属性,以及实现细节,并给外部提供使用的接口 学习封装的目的:就是为了能够限制外界对内部数据的方法 注意 :封装有隐藏的意思,但不是单纯的隐藏 pyt ...

  7. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

  8. 合肥工业大学宣城校区大学生创新创业训练项目申报书:“基于Spark平台的人工智能知识的知识图谱构建”

  9. HDU 5473 There was a kingdom 凸包 DP

    题意: 给出平面上n个点的坐标,选k个点,使得这k个点围起来的面积最大. 分析: 参考了 叉姐的分析 和 不慌不忙菊苣的代码 思路我都懂,但是DP的部分还是不太会写. 我体会了一下其中含义,也许这样可 ...

  10. UVa 12167 & HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...