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 ...
随机推荐
- 【项目相关】MVC中使用WebUploader进行图片预览上传以及编辑
项目中需要用到多图片上传功能,于是在百度搜了一下,首先使用了kissy uploader,是由阿里前端工程师们发起创建的一个开源 JS 框架中的一个上传组件...但,后面问题出现了. 在对添加的信息进 ...
- winform之回车执行某个按钮 以及Esc执行某个按钮
在winform中,我们在登陆的时候,需要点击回车键,就执行登陆,点击Esc键就执行取消,那么最方便的方法就是利用AcceptButton和CancelButton这两个属性(它属于窗体属性). 如图 ...
- Dev的DocumentManager 相关问题
1.改变DocumentManager包含的窗体的排列方式 if (this.documentManager1.View.Type != ViewType.NativeMdi) { this.docu ...
- hive中的全排序
写mapreduce程序时,如果reduce个数>1,想要实现全排序需要控制好map的输出 现在学了Hive,写sql大家都很熟悉,如果一个order by解决了全排序还用那么麻烦写mapred ...
- PHP 下载文件时自动添加bom头的方法
首先弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order ...
- setting菜单界面的形成--未优化
代码: first_preference.xml: <?xml version="1.0" encoding="utf-8"?> <Prefe ...
- 西门子MES解决方案SIMATIC IT在乳制品行业小试牛刀
竞争的白热化,紧缩的产品利润,食品安全保障,越来越苛刻的法规要求和全球化的市场与品牌维持的重要性对乳品行业的企业提出了更高的要求,实施 MES将是企业唯一的出路. 自从“十一五”制造业信息化为MES正 ...
- XAML(2) - 依赖属性
3.依赖属性 在用WPF编程时,常常会遇到"依赖属性"这个术语.WPF元素是带有方法,属性和事件的类.WPF元素的几乎每个属性都是依赖属性, 这是什么意思?依赖属性可以依赖其他输入 ...
- 创建表 添加主键 添加列常用SQL语句
--删除主键 alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段 ...
- NOJ1008-第几天
第几天 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 2701 测试通过 : 800 ...