CF div2 D BFS
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的更多相关文章
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF Two Buttons (BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 离线dfs CF div2 707 D
http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- cf div2 238 D
D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 235 D
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
随机推荐
- Redis4- llist的操作
数据类型List链表(1)介绍list类型其实就是一个双向链表.通过push,pop操作从链表的头部或者尾部添加删除元素.这使得list既可以用作栈,也可以用作队列.该list链表类型应用场景:获得最 ...
- C语言介绍
以下东东转自百度百科 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.它由美国贝尔实验室的Dennis M. Ritchie于1972年推出,1978年后,C语言已先后被 ...
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation
当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示.有两种方法可以解决以上问题: 1.修改web ...
- NSArray的containsObject漏洞
1.NSArray中的containsObject的用法 NSMutableArray *array=[NSMutableArray array]; if(![array containsObject ...
- Ckeditor配置
配置参考文档,主要将ckeditor中的(adapters.images.lang.plugins.skins.themes.ckeditor.js.config.js.contents.css)解压 ...
- ip地址的网络配置
记录一下linux下的网络配置 3.执行命令(通过ifconfig查一下网卡): vi /etc/sysconfig/network-scripts/ifcfg-eth2 注:按字母a,代表插入. 编 ...
- error: invalid 'asm': invalid operand for code 'w'
google 出结果 http://stackoverflow.com/questions/15623609/including-curl-into-the-android-aosp ........ ...
- android全屏和取消全屏 旋转屏幕
全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 取消全屏 getWindow().clearFlags(Win ...
- MySql 插入10位以上长度的字符报错or截断
当a字段为int类型时: 如果用MyBatis向MySql插入10个字符以上长度的字符串,则会报错. 如果直接在MySql中用sql语句插入10个字符以上长度的字符串,则会变成最大的int类型数值:2 ...
- linux学习的哲学层面的思考-架构
参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...