典型的bfs模拟 (广度优先搜索) ,不过有好多细节要注意,比如图中如果是  R#  走到这个R的话就无限往右走了,这样就挂了~肯定到不了出口。还有一种容易造成死循环的,比如

#E##

DLLL

D. .U

D.SU

RRRU

这样的话就必须要标记下当前位置某个方向获得钥匙的状态是否被访问过了,获得钥匙的状态可以状态压缩来表示。

墙角如果遇到转弯了是不会加step的!

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; struct Point {
int x, y, step, dir, now;
Point() {}
Point(int x, int y, int step, int dir, int now) : x(x), y(y), step(step), dir(dir), now(now) {}
}cur; queue<Point> q;
bool vis[4][1<<7][202][202];
int n, m, key, id[202][202], mp[333];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char s[202][202];
int full ; int bfs() {
full = (1<<key)-1;
while(!q.empty()) {
cur = q.front();
q.pop();
int x = cur.x, y = cur.y, dir = cur.dir, step = cur.step, now = cur.now;
int xx = x + dx[dir], yy = y + dy[dir];
if(xx < 1 || yy < 1 || xx > n || yy > m) continue;
// 模拟走的路程,细节要注意
while(s[xx][yy] != '#') {
x = xx; y = yy;
if(s[xx][yy] == 'K') {
now |= id[xx][yy];
}
if(s[xx][yy] == 'E') {
if(now == full) return step;
}
if(mp[s[xx][yy]] >= 0) {
dir = mp[s[xx][yy]];
if(vis[dir][now][xx][yy]) break;
vis[dir][now][xx][yy] = 1;
}
xx += dx[dir]; yy += dy[dir];
if(xx < 1 || yy < 1 || xx > n || yy > m) break;
}
if(xx < 1 || yy < 1 || xx > n || yy > m) continue;
if(s[xx][yy] == '#' && mp[s[x][y]] == -1) {
for(int i = 0;i < 4; i++) if(i != dir) {
if(vis[i][now][x][y]) continue;
vis[i][now][x][y] = 1;
xx = x+dx[i];yy = y + dy[i];
if(s[xx][yy] == '#') continue;
cur = Point(x, y, step+1, i, now);
q.push(cur);
}
}
}
return -1;
} int main(){
memset(mp, -1, sizeof(mp));
mp['D'] = 0; mp['U'] = 1; mp['R'] = 2 ; mp['L'] = 3;
int i, j, x, y, k, l;
while(scanf("%d%d", &n, &m) != -1) {
for(i = 1;i <= n; i++)
scanf("%s", s[i]+1);
key = 0;
for(i = 1;i <= n; i++) {
for(j = 1;j <= m; j++)
if(s[i][j] == 'K') {
id[i][j] = 1<<key;
key++;
}
else if(s[i][j] == 'S')
x = i, y = j;
}
memset(vis ,0, sizeof(vis));
while(!q.empty()) q.pop();
for(i = 0;i < 4; i++) {
cur = Point(x, y, 1, i, 0);
vis[i][0][x][y] = 1;
q.push(cur);
}
printf("%d\n", bfs());
}
return 0;
}

hdu 4634 Swipe Bo 搜索的更多相关文章

  1. HDU 4634 Swipe Bo (2013多校4 1003 搜索)

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. hdu 4634 Swipe Bo bfs+状态压缩

    题目链接 状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索. 搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题. #include <iostream> #inc ...

  3. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  4. hdu Swipe Bo(bfs+状态压缩)错了多次的题

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. HDU 5752 Sqrt Bo (数论)

    Sqrt Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...

  6. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  7. HDU 5762 Teacher Bo (暴力)

    Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  8. HDU 4616 Game (搜索)、(树形dp)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...

  9. [HDU 2102] A计划(搜索题,典型dfs or bfs)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. App在后台运行

    App有三种状态: 1. 死亡状态(未打开App); 2. 前台运行状态(打开状态); 3. 后台暂停状态(停止所有动画, 定时器, 多媒体联网等操作) 4. 后台运行状态(后台运行); ------ ...

  2. hdu 4454 Stealing a Cake

    简单的计算几何: 可以把0-2*pi分成几千份,然后找出最小的: 也可以用三分: #include<cstdio> #include<cmath> #include<al ...

  3. PYTHON--CLASS

    class Robot: population = 0 def __init__(self, name): self.name = name print("(Initializing {0} ...

  4. OA学习笔记-009-岗位管理的CRUD

    一.分析 Action->Service->Dao CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承 ...

  5. go网络编程示例,客户端,服务器端

    http://blog.csdn.net/wangningyu/article/details/22859245 http://blog.csdn.net/wangningyu/article/det ...

  6. Perl脚本学习经验(四)--Perl中sftp的使用

    使用sftp,需要使用Except模块,该模块需要下载安装在perl目录下,可以上http://www.cpan.org/上下载对应的安装包:1. 用root用户登录环境:2. cd /usr/lib ...

  7. 【转】c++重载、覆盖、隐藏——理不清的区别

    原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...

  8. 【转】 Android——eclipse共享library以及导出jar包

    原文网址:http://blog.csdn.net/jscese/article/details/36627195 android的apk在在eclipse上进行开发的时候,有时候需要import其它 ...

  9. C# Protect the Password inside a TextBox ZZ

    If the Text property is called, it will send an WM_GETTEXT message, so it will surely be an internal ...

  10. 剪花布条 --HDOJ 2087

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...