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. tableview cell添加3D动画

    当cell显示之前,会先调用该方法,因此给cell添加动画,在这个方法里面即可. -(void)tableView:(UITableView *)tableView willDisplayCell:( ...

  2. hdu_4734_F(x)(数位DP水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:给你一个F(x)的定义,然后给你a,b,问你在0到b包括b有多少个数满足F(x)<= ...

  3. putty 直接连 快捷键方式

    快捷方式 : "C:\Program Files (x86)\puTTY\putty.exe" root@linux.9hlh.com d:\soft\putty.exe -pw ...

  4. 经典.net面试题目(2)

    101.在.net(C# or vb.net)中如何取消一个窗体的关闭. 答:private void Form1_Closing(object sender, System.ComponentMod ...

  5. javascript操作json

    for (var i = 0; i < selectedPartList.length; i++) { if (selectedPartList[i].vpart_code == jsonRow ...

  6. MyEclipse运行到断点也跳过的问题

    如果是B/S开发也就是javaWeb开发的话,Tomcat 的启动模式要设置成Debug模式 还有下面是没运行时断点的样子: 运行的时候,断点会变成对钩,表示执行到它所在代码的时候会停下来:

  7. 使用 Eclipse Memory Analyzer 进行简单内存泄漏分析

    Java 内存泄露的根本原因: 保存了不可能再被访问的变量类型的引用.因此我们的目的就是要找出这样的引用. 1.测试代码: public class MainActivity extends Acti ...

  8. Spring.NET 中的 ADO.NET 数据访问的示例

    Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spri ...

  9. 在Linux系统如何让程序开机时自动启动

    在Linux系统如何让程序开机时自动启动      核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...

  10. 在MyEclipse中运行tomcat报错 严重: Error starting static Resources

    严重: Error starting static Resourcesjava.lang.IllegalArgumentException: Document base E:\apache-tomca ...