Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接:
http://codeforces.com/contest/676/problem/D
题意:
如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终点坐标xm,ym,现在你可以选择在原地把地图上所有格子顺时针旋转90度;或者往上下左右走一格,问走到终点的最短路。
题解:
三维的bfs最短路,就是写起来比较麻烦。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
using namespace std; const int maxn = ;
int n, m; char str[][maxn][maxn]; char mp[],dir[][];
void get_mp(){
memset(mp, , sizeof(mp));
mp['+'] = '+';
mp['-'] = '|';
mp['|'] = '-';
mp['^'] = '>';
mp['>'] = 'v';
mp['v'] = '<';
mp['<'] = '^';
mp['L'] = 'U';
mp['U'] = 'R';
mp['R'] = 'D';
mp['D'] = 'L';
mp['*'] = '*'; memset(dir, , sizeof(dir));
dir['+'][] = dir['+'][] = dir['+'][] = dir['+'][] = ;
dir['-'][]=dir['-'][]=;
dir['|'][]=dir['|'][]=;
dir['^'][]=;
dir['>'][]=;
dir['v'][]=;
dir['<'][]=;
dir['L'][]=dir['L'][]=dir['L'][]=;
dir['U'][]=dir['U'][]=dir['U'][]=;
dir['R'][]=dir['R'][]=dir['R'][]=;
dir['D'][] = dir['D'][] = dir['D'][] = ;
} struct Node {
int s, x, y;
Node(int s, int x, int y) :s(s), x(x), y(y) {}
Node() {}
}; int xt, yt, xm, ym;
int d[][maxn][maxn];
int vis[][maxn][maxn];
int bfs() {
memset(d, 0x3f, sizeof(d));
memset(vis, , sizeof(vis));
d[][xt][yt] = ;
queue<Node> Q;
Q.push(Node(, xt, yt)); vis[][xt][yt] = ;
while (!Q.empty()) {
Node nd = Q.front(); Q.pop();
int s = nd.s, x = nd.x, y = nd.y;
if (x == xm&&y == ym) return d[s][x][y];
int ss, xx, yy;
ss = (s + ) % , xx = x, yy = y;
if (!vis[ss][xx][yy]) {
d[ss][xx][yy] = d[s][x][y] + ;
Q.push(Node(ss, xx, yy));
vis[ss][xx][yy] = ;
}
ss = s, xx = x - , yy = y;
if (!vis[ss][xx][yy]&&xx>=&&dir[str[ss][xx][yy]][]&&dir[str[s][x][y]][]) {
d[ss][xx][yy] = d[s][x][y] + ;
Q.push(Node(ss, xx, yy));
vis[ss][xx][yy] = ;
}
ss = s, xx = x + , yy = y;
if (!vis[ss][xx][yy] && xx <=n && dir[str[ss][xx][yy]][] && dir[str[s][x][y]][]) {
d[ss][xx][yy] = d[s][x][y] + ;
Q.push(Node(ss, xx, yy));
vis[ss][xx][yy] = ;
}
ss = s, xx = x, yy = y-;
if (!vis[ss][xx][yy] && yy >= && dir[str[ss][xx][yy]][] && dir[str[s][x][y]][]) {
d[ss][xx][yy] = d[s][x][y] + ;
Q.push(Node(ss, xx, yy));
vis[ss][xx][yy] = ;
}
ss = s, xx = x, yy = y + ;
if (!vis[ss][xx][yy] && yy <= m && dir[str[ss][xx][yy]][] && dir[str[s][x][y]][]) {
d[ss][xx][yy] = d[s][x][y] + ;
Q.push(Node(ss, xx, yy));
vis[ss][xx][yy] = ;
}
}
return -;
} int main() {
get_mp();
while (scanf("%d%d", &n, &m) == && n) {
for (int i = ; i <= n; i++) scanf("%s", str[][i]+);
for (int i = ; i <= ; i++) {
for (int j = ; j <= n; j++) {
for (int k = ; k <= m; k++) {
str[i][j][k] = mp[str[i - ][j][k]];
}
}
}
scanf("%d%d%d%d", &xt, &yt, &xm, &ym);
printf("%d\n", bfs());
}
return ;
}
Codeforces Round #354 (Div. 2) D. Theseus and labyrinth的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2)
贪心 A Nicholas and Permutation #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 ...
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #354 (Div. 2)-A
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
随机推荐
- MySQL语法
sql(structure query language)结构化查询语言ansi iso/iec组织制定ddl(data definition language) 数据定义语言dml(manipula ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- C# 运行时编辑 节点重命名
方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...
- Web 应用程序项目 MvcApplication1 已配置为使用 IIS。
今天网上下了一个项目,加载不了,并报如下错误: Web 应用程序项目 MvcApplication1 已配置为使用 IIS. 若要访问本地 IIS 网站,必须在管理员帐户的上下文中运行 Visual ...
- 用,隔开sql临时表
IF OBJECT_ID('[kkd].[proc_kkd_GetAutoExamineBid]') IS NOT NULL BEGIN DROP PROC [kkd].[proc_kkd_GetAu ...
- JQuery ajax返回JSON时的处理方式
最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...
- 记录C++学习历程
从今天开始学习C++,将学习中遇到的问题,以及解决方案记录在这个博客里. 函数 1.C++函数声明(原型) 函数原型跟函数的定义在返回值类型,函数名,参数上必须完全一致. 2.程序的内存区域:全局数据 ...
- 关于frameset中指定区域回退的实现
指定区域(Frame)的回退,网上大都写的是用 window.parent.window.mainFrame.rightFrame.history.back();来进行回退,但是我这边就是不行,一直 ...
- wing IDE破解方法
WingIDE是我接触到最好的一款Python编译器了.但其属于商业软件,注册需要一笔不小的费用.因此,这篇简短的文章主要介绍了破解WingIDE的方法.破解软件仅供学习或者教学使用,如果您是商业使用 ...
- mq消息队列
rabbitmq学习9:使用spring-amqp发送消息及同步接收消息 通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的.有兴趣的朋友 ...