最短路。

$dis[i][j][k]$记录到点$(i,j)$,门的状态为$k$时的最短路。转移的时候有$8$种方案,即直接走向周围四个点,或者进行旋转。比较烦的是判断两个相邻的点在状态$k$下是否连通,仔细一些就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int INF=0x7FFFFFFF;
const int maxn=;
int n,m,sx,sy,ex,ey;
char s[maxn][maxn];
int dir[][]={ {-,},{,},{,-},{,} };
int dis[maxn][maxn][];
bool f[maxn*maxn*];
int tmp[],tt[],p1[],p2[]; struct X
{
int st,dis;
X(int ST,int DIS){ st=ST; dis=DIS;}
bool operator <(const X&a) const
{
return dis>a.dis;
}
}; bool p(int x,int y)
{
if(x<||x>=n) return ;
if(y<||y>=m) return ;
if(s[x][y]=='*') return ;
return ;
} void get(int a,int b,int st)
{
memset(tmp,,sizeof tmp);
memset(tt,,sizeof tt); if(s[a][b]=='+') tmp[]=,tmp[]=,tmp[]=,tmp[]=;
else if(s[a][b]=='-') tmp[]=,tmp[]=;
else if(s[a][b]=='|') tmp[]=,tmp[]=; else if(s[a][b]=='^') tmp[]=;
else if(s[a][b]=='>') tmp[]=;
else if(s[a][b]=='v') tmp[]=;
else if(s[a][b]=='<') tmp[]=; else if(s[a][b]=='U') tmp[]=,tmp[]=,tmp[]=;
else if(s[a][b]=='R') tmp[]=,tmp[]=,tmp[]=;
else if(s[a][b]=='D') tmp[]=,tmp[]=,tmp[]=;
else if(s[a][b]=='L') tmp[]=,tmp[]=,tmp[]=; for(int i=;i<;i++) tt[(i+st)%]=tmp[i];
} bool check(int x1,int y1,int x2,int y2,int st)
{
get(x1,y1,st); for(int i=;i<;i++) p1[i]=tt[i];
get(x2,y2,st); for(int i=;i<;i++) p2[i]=tt[i]; if(x1==x2)
{
if(y1+==y2)
{
if(p1[]==&&p2[]==) return ;
return ;
}
else
{
if(p1[]==&&p2[]==) return ;
return ;
}
} else if(y1==y2)
{
if(x1+==x2)
{
if(p1[]==&&p2[]==) return ;
return ;
}
else
{
if(p1[]==&&p2[]==) return ;
return ;
}
} return ;
} void spfa()
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
for(int k=;k<;k++)
dis[i][j][k]=INF; dis[sx][sy][]=;
priority_queue<X>Q; Q.push(X(sx*m+sy,)); while(!Q.empty())
{
X top=Q.top(); Q.pop();
int st,r,c,h=top.st;
st=h/(n*m); h=h-st*(n*m); r=h/m; c=h%m; if(dis[r][c][st]<top.dis) continue; for(int i=;i<;i++)
{
int t=(st+i)%;
if(dis[r][c][st]+i<dis[r][c][t])
{
dis[r][c][t]=dis[r][c][st]+i;
Q.push(X(r*m+c+t*n*m,dis[r][c][t]));
}
} for(int i=;i<;i++)
{
int tx=r+dir[i][],ty=c+dir[i][];
if(p(tx,ty)==) continue;
if(check(r,c,tx,ty,st)==) continue; if(dis[r][c][st]+<dis[tx][ty][st])
{
dis[tx][ty][st]=dis[r][c][st]+;
Q.push(X(tx*m+ty+st*n*m,dis[tx][ty][st]));
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) scanf("%s",s[i]);
scanf("%d%d",&sx,&sy); sx--; sy--;
scanf("%d%d",&ex,&ey); ex--; ey--;
spfa();
int ans=INF;
for(int i=;i<;i++) ans=min(ans,dis[ex][ey][i]);
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
return ;
}

CodeForces 676D Theseus and labyrinth的更多相关文章

  1. codeforces 676D Theseus and labyrinth BFS搜索

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

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

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

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

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

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

  5. CodeForces 676D代码 哪里有问题呢?

    题目: http://codeforces.com/problemset/problem/676/D code: #include <stdio.h> #define MAXN 1001 ...

  6. 【CF679B】Theseus and labyrinth(数学,贪心)

    题意: 给一个m<=10^15,每次都减最接近当前值的立方数 让你找一个不大于m的最大的数并且这个数是减法次数最多的数 思路:见http://blog.csdn.net/miracle_ma/a ...

  7. 【CF676D】Theseus and labyrinth(BFS,最短路)

    题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每 ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #354 (Div. 2)-D

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

随机推荐

  1. SugarSync的API总结

    SugarSync API App支持SugarSync网盘的前提: 1.AccessKeyID:xxx 2.Private Access Key:xxx 3.AppID:xxx 详细的API总结如下 ...

  2. ruby gsub gsub! chomp chomp! 以及所有类似函数用法及区别

    ruby中带“!"和不带"!"的方法的最大的区别就是带”!"的会改变调用对象本身了.比方说str.gsub(/a/, 'b'),不会改变str本身,只会返回一个 ...

  3. CSS 选择器的学习

    从蛋学习 CSS 选择器 + 做一个 Jumony 桌面工具,真费劲,目前Jumony Tool两大问题无法解决,完美的显示 html 文档和根据结果在文档中定位,有点儿跑题了…… 1. 选择“cla ...

  4. CentOS上搭建java WEB开发环境Tomcat+MySQL+JDK

    对于初学者来说,想在linux系统上搭建一个java web服务器,不知道什么方案可行, 这篇文章主要是告诉这些基础和概念相对薄弱的同学,这样搭建是可行的,大体上没问 题的,出问题也是细节问题.所以此 ...

  5. 增加窗体边框3D效果

    /// <summary> /// 增加窗体边框3D效果 /// </summary> /// <param name="e"></par ...

  6. Use weechat (IRC client) on OS X. MacBook Pro

    Weechat is a console IRC client. It is opensource and very easy to use. I use weechat in my Linux PC ...

  7. [基础]RHEL6下LINUX服务器批量部署

      包准备:xinetd,tftp-server,dhcp,httpd,system-config-kickstart,syslinux,nfs   试验环境: 本机地址:192.168.46.98 ...

  8. 利用命令行来安装应用到android虚拟机

    1.首先将将android  sdk中的adb添加到环境变量中确保在任意目录下运行adb都可以执行 2.将命令行切换到应用安装包所在未知 3.运行:adb install 应用名称即可

  9. [置顶] Java开源代码研究总结

          由于工作中的需要,最近在研究SNMP协议和利用snmp4j和snmp4j.agent(   http://www.snmp4j.org/ ),实现snmp的南向和北向功能. 结合以前看过的 ...

  10. Elasticsearch中doc_value的认识

    前言:本文的目的是为后续磁盘空间利用优化做铺垫.主要知识点来源于官网文档 一.doc_value是什么 绝大多数的fields在默认情况下是indexed,因此字段数据是可被搜索的.倒排索引中按照一定 ...