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 ...
随机推荐
- WAMP环境下访问PHP提示下载PHP文件
原因是服务器没有加载到PHP文件 到http.conf下加载 AddType application/x-httpd-php .php AddType application/x-httpd-php ...
- input元素的padding border margin的区别
padding内(不包含padding)的部分才是可输入部分,也是width和height标明的区域.padding的部分加上width和height部分是background的部分.padding的 ...
- 内存详解--理解 JVM 如何使用 AIX 上的本机内存
转自---http://www.ibm.com/developerworks/cn/java/j-nativememory-aix/
- 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期
1. 概述 本章内容包括 管理非托管资源.使用IDisposable接口 以及 管理析构器和垃圾回收. 2. 主要内容 2.1 理解垃圾回收机制 ① 代码执行的时候,内存中有两个地方存放数据项:堆 和 ...
- python基础学习笔记第四天 list 元祖 字典
一 LIST方法 列表操作包含以下函数:1.cmp(list1, list2):比较两个列表的元素 2.len(list):列表元素个数 3.max(list):返回列表元素最大值 4.min(lis ...
- Ubuntu下部署java JDK和eclipse IDE
安装Java编程开发环境: Ubuntu默认安装openjava,可以通过java -version查看是否安装.但我使用Ubuntu9.10升级到10.04LTS时,openjava没有了.另外,如 ...
- jquery绑定事件失效的情况(转)
原文地址:http://www.thinksaas.cn/group/topic/348453/ jQuery中文api地址:http://www.jquery123.com/api/ jQuery官 ...
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
在android应用中,有时要加载一个网页,如果能配上一个进度条就更好了,而android 中提供了其很好的支持, 其中webView的一系列用法,比如 webView.getSettings().s ...
- python 删除list中重复元素
list = [1,1,3,4,6,3,7] 1. for s in list: if list.count(s) >1: list.remove(s) 2. list2=[] for s in ...
- [转]Ubuntu下配置NFS服务
[转]Ubuntu下配置NFS服务 http://blog.163.com/liu8821031%40126/blog/static/111782570200921021253516/ Table ...