CodeForces 676D Theseus and labyrinth
最短路。
$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的更多相关文章
- codeforces 676D Theseus and labyrinth BFS搜索
分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了 #include <cstdio> #include <iost ...
- 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 ...
- 【25.93%】【676D】Theseus and labyrinth
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- CodeForces 676D代码 哪里有问题呢?
题目: http://codeforces.com/problemset/problem/676/D code: #include <stdio.h> #define MAXN 1001 ...
- 【CF679B】Theseus and labyrinth(数学,贪心)
题意: 给一个m<=10^15,每次都减最接近当前值的立方数 让你找一个不大于m的最大的数并且这个数是减法次数最多的数 思路:见http://blog.csdn.net/miracle_ma/a ...
- 【CF676D】Theseus and labyrinth(BFS,最短路)
题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #354 (Div. 2)-D
D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...
随机推荐
- centos安装及配置
centos安装 从网上下载最新的版本,用ultraiso做好系统盘,启动.安装过程没有什么可以说的,与其他发行版有点不同的是需要将安装包放到u盘根目录下(u盘容量最好大点,8~16G最好).开始没注 ...
- 职责链模式实例(C#)
下面是使用指责链模式的一个具体的实例,可以方便的理解指责链模式. 其实指责链简单的来说,就是把一个指责的流程定义出来,像一条链路一样,每一个结点有自己的处理范围,若不能处理,则传到自己的引用的下一个结 ...
- Socket一些常用的方法封装
public class SocketHelper { /// <summary> /// 功能描述:得到一个实例对象 /// </summary> /// <retur ...
- SVN版本冲突解决详解
SVN版本冲突解决详解 分类: SVN(SubVersion)2009-11-23 15:45 27014人阅读 评论(12) 收藏 举报 svnsubversion服务器文档工作c 版本冲突原因: ...
- Linux 环境进程间通信(六):
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- 运用Unity实现依赖注入[结合简单三层实例]
运用Unity实现依赖注入[结合简单三层实例] 一:理论部分 依赖注入:这是 Ioc 模式的一种特殊情况,是一种基于改变对象的行为而不改变类的内部的接口编程技术.开发人员编写实现接口的类代码,并基于接 ...
- WPF MVVM使用prism4.1搭建
WPF MVVM使用prism4.1搭建 MVVM即Model-View-ViewModel,MVVM模式与MVP(Model-View-Presenter)模式相似,主要目的是分离视图(View)和 ...
- Mahout之(三)相似性度量
User CF 和 Item CF 都依赖于相似度的计算,因为只有通过衡量用户之间或物品之间的相似度,才能找到用户的“邻居”,才能完成推荐.上文简单的介绍了相似性的计算,但不完全,下面就对常用的相似度 ...
- 转载-windows下MySql5.6.17没有setup.exe时的安装方法
转载出处为:http://blog.csdn.net/zgrjkflmkyc/article/details/25321537 (最终,我也没有安装下述的方法安装成功,虽然有服务,但是服务启动不正常, ...
- DBCC用法汇总
本文摘自http://www.cnblogs.com/lilycnblogs/archive/2011/03/31/2001372.html 留作查阅 DBCC是SQL Server提供的一组控制台命 ...