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 ...
随机推荐
- 【ios开发】图片拉伸
最近在做一个项目 其中要自己定制一个View 如图: 但是美工给了我的图片尺寸却是不一样的. 分别是599*80 26*61 于是就成了这样的效果. 很明显的发现取消四周不对劲. 于是我就去找美工姐 ...
- jquery mobile Checkbox动态添加刷新及事件绑定
jquery mobile Checkbox动态添加刷新及事件绑定 在微信项目中,涉及到一个多选功能.数据来自后台数据库,需要动态加载. 项目结构:微信api+web app.使用jquery mob ...
- WP中一些耗时的东西
MediaPlayer.GameHasControl 耗时1.5ms MediaPlayer.State 耗时0.4ms 上面两个原本放在游戏的update中,后来注释掉发现其它的游戏逻辑只要0.2m ...
- 三种不同实现初始化和销毁bean之前进行的操作的比较
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...
- IOS开发的内存管理
关于IOS开发的内存管理的文章已经很多了,因此系统的知识点就不写了,这里我写点平时工作遇到的疑问以及解答做个总结吧,相信也会有人遇到相同的疑问呢,欢迎学习IOS的朋友请加ios技术交流群:190956 ...
- TCPDump 抓Loopback数据包
编写网络程序必备截包工具, unix下面自带tcpdump, linux就不用说了.用于排查网络程序的bug,命令行如何使用请百度谷歌.分析包推荐wireshark,可视化非常方便.一般都是在非Win ...
- Android 短信模块分析(四) MMS之短信的发送与接收
MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...
- IOS设计模式学习(7)单例
1 前言 数学与逻辑学中,singleton定义为“有且仅有一个元素的集合”.因此不管袋子有多大,每次从里面取出弹子的时候,拿到的都是同一个. 2 详述 2.1 简述 面向对象应用程序中的单例类(si ...
- List中函数用法 First,FirstOrDefault,Single,SingleOrDefault的区别
操作符 如果源序列是空的 源序列只包含一个元素 源序列包含多个元素 First 抛异常 返回该元素 返回第一个元素 FirstOrDefault 返回default(TSource) 返回该元素 返回 ...
- iOS开发-内存管理
内存管理 对于这篇呢,其实现在都是ARC模式,正常状态下基本不用我们去手动释放内存,所以如果不是要面试呀.装逼或者扎实功底的,就先别看了或者了解下即可,因为像面试时,有些面试官想看你的基础时,就有些人 ...