http://codeforces.com/contest/676/problem/D

题目大意:

勇者去迷宫杀恶龙。迷宫是有n*m的方格子组成的。迷宫上有各种记号,这些记号表达着能走的方向。当且仅当两个房间的记号是相互联通的,才能走过去。

我们有如下选择,最直白的一种就是直接走到另外一个格子上去(当然格子得相互连通),第二种操作就是顺时针旋转所有的格子(格子的位置保持不变)。

问,勇者要多久才能走到恶龙所在的地方。

思路:

表示这道题真心不会。。。下面的这个代码也是参考了别人的吧。。。换成我的话for的循环里面肯定不会写成f*3+i之类的(虽然这个到现在还没有弄明白)

我们用结构体保存当前的位置,旋转的次数和行走的距离。然后我们每次对一个当前的格子进行两种操作

①瞬时间旋转操作(每次都旋转1,反正最后肯定会遍历到4的)

②就是枚举上下左右,看看是否能走过去。

然后就OK了

 //看看会不会爆int!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define all(a) a.begin(), a.end() const int maxn = ;
bool vis[maxn][maxn][];
int n, m;
struct point{
int x, y;
int f, d;
point(int xx = , int yy = , int ff = , int dd = ){
x = xx, y = yy, f = ff, d = dd;
}
}p[maxn][maxn]; char atlas[maxn][maxn];
int xt, yt, xm, ym;
/*
int dx[] = {0, 0, -1, 1};//上下左右
int dy[] = {-1, 1, 0, 0};
*/
int dx[] = {-, , , };//左下右上
int dy[] = {, , , -}; bool check(char ch, int dir){
if (dir == ) return ch == '+' || ch == '|' || ch == '^' || ch == 'L' || ch == 'R' || ch == 'D';
else if (dir == ) return ch == '+' || ch == '-' || ch == '>' || ch == 'L' || ch == 'U' || ch == 'D';
else if (dir == ) return ch == '+' || ch == '|' || ch == 'v' || ch == 'L' || ch == 'R' || ch == 'U';
else return ch == '+' || ch == '-' || ch == '<' || ch == 'R' || ch == 'U' || ch == 'D';
return ;
} int bfs(){
queue <point> que;
vis[xt][yt][] = ;
que.push(point(xt, yt, , ));
while (!que.empty()){
point q = que.front();
que.pop();
int x = q.x, y = q.y;
int f = q.f, d = q.d;
if (x == xm && y == ym) return d;
if (!vis[x][y][(f + ) % ]){
vis[x][y][(f + ) % ] = true;
que.push(point(x, y, (f + ) % , d + ));
}
for (int i = ; i < ; i++){
int nx = x + dx[i], ny = y + dy[i];
if (nx <= || ny <= || nx > n || ny > m) continue;
if (atlas[nx][ny] == '*' || vis[nx][ny][f]) continue;
if (!check(atlas[x][y], (f * + i) % )) continue;//我们要选择的是和上面相反的方向
if (!check(atlas[nx][ny], (f * + i + ) % )) continue;
que.push(point(nx, ny, f, d + ));
vis[nx][ny][f % ] = true;
}
}
return -;
} int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%s", atlas[i] + );
}
scanf("%d%d", &xt, &yt);
scanf("%d%d", &xm, &ym);
int ans = bfs();
printf("%d\n", ans);
return ;
}
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
*/

CF div2 D BFS的更多相关文章

  1. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. CF Two Buttons (BFS)

    Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. 离线dfs CF div2 707 D

    http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...

  4. cf div2 239 D

    D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. cf div2 236 D

    D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  7. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. cf div2 235 D

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

随机推荐

  1. Python 修炼1

    2016年11月21日 Python基础修炼第一篇 1.Python是什么?有什么优缺点呢? python是一个高级编程语言. 优点:开发效率比较高,不但有php写网页的功能,还有写后台的功能 缺点: ...

  2. 学习笔记——解释器模式Interpreter

    解释器模式,其实就是编译原理中的语法解释器,如果用在项目中,可以用于实现动态脚本的解析,也就是说项目可以支持用户脚本扩展. 但实际上,这种运行时解释,效率很慢,如果不是很需要的话,不建议使用. 一种简 ...

  3. Load$$ execution region symbols

    6.3.3 Load$$ execution region symbols The linker generates Load$$ symbols for every execution region ...

  4. Python2中while 1比while True更快

    1) bool类是从int类继承而来的 2) True/False 在python2中不是关键字,但是在python3是(True,False,None) PS > python2 Enthou ...

  5. HIT Winter Day ACM入门

    A. Arpa’s hard exam and Mehrdad’s naive cheat 题意:统计1378^n的末尾数字 即统计8^n的末尾数字 n=0时为1 其他情况为{8,4,2,6}中的一个 ...

  6. raise()函数

    kill和raise函数用来发送信号, 区别在于: kill把信号发送给进程或进程组. kill(pid_t pid, int signo) raise把信号发送给进程自己,相当于 raise(ing ...

  7. Spring Security3实现,权限动态获取

    Spring Security3实现,权限动态获取 原文  http://blog.csdn.net/yangwei19680827/article/details/9359113 主题 网络安全Sp ...

  8. 兼容IE低版本

    1,IE6PNG透明的bug,只需要把png图另存为无杂边的png-8格式 2,在IE6用overflow:hidden清除浮动,要加上zoom:1 3,IE6下盒子的最小高度为20px 如果要小于2 ...

  9. Hadoop 中关于 map,reduce 数量设置

    map和reduce是hadoop的核心功能,hadoop正是通过多个map和reduce的并行运行来实现任务的分布式并行计算,从这个观点来看,如果将map和reduce的数量设置为1,那么用户的任务 ...

  10. php过滤提交数据 防止sql注入攻击

    规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...