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 ...
随机推荐
- 前端javascript发送ajax请求、后台书写function小案例
HTML端页面: <td> <input class="pp_text" type="text" name="" valu ...
- JS调用腾讯接口获取天气
想做个直接通过JS获取某个城市的天气.本来想通过直接调用中国气象网的接口: http://www.weather.com.cn/weather/101070201.shtml,但是跨域问题一直无法解决 ...
- MySql like模糊查询使用详解
一.SQL的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零个字符).在 MySQL中,SQL的模式缺省是忽略大小写的.下面显示一些例子.注意在你使用SQL模式时,你不能使用 ...
- IBM开发者 JSON 教程
在异步应用程序中发送和接收信息时,可以选择以纯文本和 XML 作为数据格式.掌握 Ajax 的这一期讨论另一种有用的数据格式 JavaScript Object Notation(JSON),以及如何 ...
- VMware虚拟机升级过程中遇到的一点问题
在将VWware由9.0升级到10.0的过程中,出现如下图的错误: failed to create the requested registry key Key:Installer e ...
- web.xml中常见配置解读
文章转自:http://blog.csdn.net/sdyy321/article/details/5838791 有一般XML都必须有的版本.编码.DTD <web-app>下子元素&l ...
- delphi常用函数过程
数据类型转化 1.1. 数值和字符串转化 Procedure Str(X [: Width [ : Decimals ]]; var S); 将数值X按照一定格式转化成字符串S.Wid ...
- FileUpload控件「批次上传 / 多档案同时上传」的范例--以「流水号」产生「变量名称」
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/19/multiple_fileupload_asp_net_20130819. ...
- Oracle自用脚本(持续更新)
--查询Oracle正在执行的sql语句及执行该语句的用户 SELECT b.sid oracleID, b.username 登录Oracle用户名, b.serial#, spid 操作系统ID, ...
- mamp pro
MAMP PRO Settings and Files /Library/Application Support/appsolute/MAMP PRO ~/Library/Application Su ...