典型的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. Toy Storage

    Toy Storage 题型与2318 TOYS一样,注意要对线段排序,现在模板又更新了~~ #include<iostream> #include<cstdio> #incl ...

  2. 将页面中指定表格的数据导入到Excel中

    function AutoExcel(){   var oXL = new ActiveXObject("Excel.Application"); //创建应该对象   var o ...

  3. iOS - instancetype

    OC是一门正在迅速发展的语言,ARC,object literals ,subscripting ,blocks,Auto Synthesis,让我们看到它惊人的改变.instancetype是cla ...

  4. IOS webview中cookie的读取与保存-b

    Cookie 的读取 将它放在 webViewDidFinishLoad 开始后执行 NSArray *nCookies = [[NSHTTPCookieStorage sharedHTTPCooki ...

  5. mooc

    Coursera 课程来源 2014年前已与斯坦福.普林斯顿等近90所大学和教育机构达成合作关系. 用户类型 主要类别为学生.求职者.公司人.其中,求职者可在Coursera上获得<成就报告&g ...

  6. WordPress 前端投稿/编辑插件 DJD Site Post(支持游客和已注册用户)

    转自:http://www.wpdaxue.com/front-end-publishing.html 说到前端用户投稿,倡萌之前推荐过3个不错的插件: WordPress匿名投稿插件:DX-Cont ...

  7. 由tomcat启动想到的

      1.batch:批处理文件,表示一批 2.profile:轮廓 3.用户变量和系统变量的关系是什么?     答:点击"我的电脑→属性→高级"标签的"环境变量&quo ...

  8. 在PYTHON中,用cx_Oracle连接ORACLE数据库简单示例

    一,在安装的时候,参数有点不一样: python setup.py build install 二,连接数据库,有两种方式,DSN和TNSNAMES方式: #dsn = orcl.makedsn(se ...

  9. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

  10. 子查询解嵌套not in 无法展开改写

    SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...