题意:给定一张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. PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0  ...

  2. bootstrap 翻页的状态

    翻页的状态 下面的实例演示了上表中所讨论的 class .disabled 的用法: <!DOCTYPE html><html><head><meta htt ...

  3. C语言单链表的实现

    // //  main.c //  gfhjhgdf // //  Created by chenhao on 13-12-23. //  Copyright (c) 2013年 chenhao. A ...

  4. 公用表表达式CTE简单递归使用-简单树形结构

    1.建表脚本 CREATE TABLE [dbo].[tb_tree]( ,) NOT NULL, [ParentId] [int] NULL, ) NULL, CONSTRAINT [PK_tb_t ...

  5. setup/hold 分析

    分析说明:D2:目的寄存器:D1:源寄存器: edge2:下一个时钟上升沿:edge1:当前时钟上升沿:edge0:当前时钟上升沿的前一个时钟沿:如下图: 建立时间:触发器D2(数据要到达目的的地方) ...

  6. windows 2008r2+php5.6.28环境搭建详细过程

    安装IIS7 安装php 网站验证 安装IIS7 1.打开服务器管理器(开始-计算机-右键-管理-也可以打开),添加角色 直接下一步 勾选Web服务器(IIS),下一步,有个注意事项继续下一步(这里我 ...

  7. html5音频audio对象封装成vue组件的方式调用以及setTimeout如何在vue2生效 (vue2正在熟悉中,ajax还是用jQuery来写舒服些,里面含有一些php写法可略过) 此网页应用在PC不考虑手机端

    // vue2 组件封装如下: <template> <div> <div><!--vue element 组件的引用 Switch 开关 不懂请自行百度(重 ...

  8. LeetCode(189) Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  9. local search——配图

  10. launchMode

    launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的Activity实例,是否和其他Activity实例公用一个task ...