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 ...
随机推荐
- Yii2.0的安装与配置教程
版权声明:本文为博主原创文章,未经博主允许不得转载. PHP版本需求:PHP5.4.0以上,因为Yii2.0基于PHP5.4以上版本进行了完全重写. 目前有两种方法可以安装Yii2.0,一种是安装Co ...
- ORACLE小工具:存储过程清空所有表或使所有触发器失效
清空所有表: CREATE OR REPLACE PROCEDURE CLEAN_TABLES as v_tablename varchar2(256); cursor cur_tablename i ...
- PHPExcel解决内存占用过大问题-dw 查找memoryCacheSize把1M改为2048M
http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.html PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理E ...
- 第四十二节,configparser特定格式的ini配置文件模块
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...
- 交互式shell和非交互式shell的区别
交互式模式就是shell等待你的输入,并且执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.签退.当你签退后,shell也终止 ...
- Entity Framework技巧系列之十四 - Tip 56
提示56. 使用反射提供程序编写一个OData Service 在TechEd我收到一大堆有关将数据作为OData暴露的问题. 到目前为止你大概知道可以使用数据服务与Entity Framework将 ...
- OpenCV ——背景建模之CodeBook(1)
1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...
- CentOS + EPEL YUM源地址
[bizosv] name=bizsov-centos-$releasever - centos baseurl=http://yikat:yikat@download.bizsov.com/ gpg ...
- Concepts and Tricks In CNN
转自:http://blog.cvmarcher.com/posts/2015/05/17/cnn-trick/ 这篇文章主要讲一下Convolutional Neural Network(CNN)里 ...
- Qt Quick 简单教程
上一篇<Qt Quick 之 Hello World 图文详解>我们已经分别在电脑和 Android 手机上运行了第一个 Qt Quick 示例—— HelloQtQuickApp ,这篇 ...