典型的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. 如何写一个像btgoogle一样的12306泄露数据查询

    demo地址:http://www.btgoogle.com/12306/ 圣诞节,12306送给了我们一个大礼物.大约 14w的数据泄露, 看网上都沸沸扬扬的.开始也准备找一个数据库来看看,随后,我 ...

  2. ubuntu apt-get常用命令的使用

             packagename指代为软件包的名称 apt-get install packagename     安装一个新软件包(参见下文的aptitude) apt-get remove ...

  3. 微软CSS面试全记录

    先是会有一轮简单的电话技术面试,聊的比较随意,什么都会问,跟职位相关的都有.然后会发一些材料说是要学习,是windows内存管理相关的东西. 完了就是一轮oral test,和技术没有任何关系,问问为 ...

  4. 可以用来开发h5的软件小结

    webStorm phoneGap notepad++ eclips text sublime dreamWeaver intellij idea 学习h5 需要掌握的  大块的知识 xhtml ja ...

  5. jedis源码阅读

    package redis.clients.jedis; import java.util.ArrayList; import java.util.HashSet; import java.util. ...

  6. hibernate的formula如何使用

    之前用过hibernate的formula记得很好用,但是这次用到想不起来怎么用了,结果去网上查结果发现大多都是无用信息. 最终搞定了,还是在这里记录一下,省的忘记. 我用formula的目的在于字典 ...

  7. canvas仿黑客帝国的字符下落

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  8. C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查)

    一.开发环境 ReadHat6.3 32位.mysql5.6.15.gcc4.4.6 二.编译 gcc -I/usr/include/mysql -L/usr/lib -lmysqlclient ma ...

  9. 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间

    刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html  突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...

  10. jsp EL表达式 字符串的比较

    jsp EL表达式 字符串的比较 跟JavaScript一样,直接使用两个等于号即可:== 代码如下: <c:if test="${highLight == 'visa'}" ...