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 ...
随机推荐
- rebbitmq-RPC(C#)
RPC(Remote Procedure Call Protocol)——远程过程调用协议 运行时,一次客户机对服务器的RPC调用,其内部操作大致有如下十步: 1.调用客户端句柄:执行传送参数 2.调 ...
- ps -aux中的time 的意思
ps -aux 显示所有包含其他使用者的行程 aux 输出格式 : USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND USER: 行程拥有者 ...
- MySQLdb/mysql-python安装时EnvironmentError: mysql_config not found
代码:root@vpser:~# cd MySQL-python-1.2.3root@vpser:~/MySQL-python-1.2.3# python setup.py install sh: m ...
- 【CSS】圆角阴影边框CSS
.someClassName { width:300px; display: inline-block; padding: 5px 10px 6px; text-decoration: none; b ...
- EntityFramework5提供的迁移工具
目录 背景之前是如何做的?EntityFramework5提供了更好的选择备注 背景返回目录 刚毕业做项目的时候,没有用“迁移”这个概念,系统发布和更新的过程让人非常痛苦,在学习 Ruby On Ra ...
- LLVM小结
随笔- 5 文章- 0 评论- 10 LLVM小结 如果说gcc是FSF的传奇,llvm就是Chris Lattner的小清新.当然啦,想具体看看这位四处游山玩水还GPA 4.0的大神和他的 ...
- Pig性能优化
Pig性能优化 1. 尽早去除无用的数据 MapReduce Job的很大一部分开销在于磁盘IO和数据的网络传输,如果能尽早的去除无用的数据,减少数据量,会提升Pig的性能. 1). 尽早的使用Fil ...
- [转载]expect spawn、linux expect 用法小记
原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...
- ASP.NET MVC 异步Excel数据选择导出
以前习惯用一些框架来实现Excel文件数据导出,工作中也经常用到:比如extJs.easyUI.angularJs等,最近在做mvc程序的时候要实现该功能,相信这种功能在我们实际工作中是很常见,尤其是 ...
- 用C++实现斐波那契数列
我是一个C++初学者,控制台输出斐波那契数列. 代码如下: //"斐波那契数列"V1.0 //李国良于2017年1月12日编写完成 #include <iostream> ...