典型的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. IP,TCP,UDP Checksum校验

    IP数据报的校验: IP数据报只需要对数据头进行校验,步骤如下: 将接收到的数据的checksum字段设置为0 把需要校验的字段的所有位划分为16位(2字节)的字 把所有16位的字相加,如果遇到进位, ...

  2. bzoj 2706: [SDOI2012]棋盘覆盖 Dancing Link

    2706: [SDOI2012]棋盘覆盖 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 255  Solved: 77[Submit][Status] ...

  3. 限制sqlserver最大内存后无法连接-EXEC sp_configure max server memory

    在sql server 中设置了过小的 "max server memory"最大内存后,sqlserver可启动,但是无法连接. 网络上流行的"sqlserver 内存 ...

  4. poj 1273

    网络流之最大流的基础题: 可以使用dinic算法和EK算法: 分别对着模板敲了一遍: dinic: #include<cstdio> #include<cstring> #in ...

  5. comet ajax轮询

    http://www.ibm.com/developerworks/cn/webservices/ws-tip-jaxwsrpc.html http://www.cnblogs.com/pifoo/a ...

  6. Logstash同步Oracle数据到ElasticSearch

    最近在项目上应用到了ElasticSearch和Logstash,在此主要记录了Logstash-input-jdbc同步Oracle数据库到ElasticSearch的主要步骤,本文是对环境进行简单 ...

  7. 《转》SQL Server 2008 数据维护实务

    SQL Server 2008 数据维护实务 http://blog.csdn.net/os005/article/details/7739553 http://www.cnblogs.com/xun ...

  8. MST(prim)+树形dp-hdu-4756-Install Air Conditioning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4756 题目意思: n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个 ...

  9. bzoj1016

    这道题主要利用了最小生成树的两个性质 最小生成树每种边权的数目固定不变 最小生成树每种边权带来的连通状况一定唯一 由于每种边权的只有不到10种,所以直接穷举然后乘法原理即可 ; type node=r ...

  10. NOI2005瑰丽华尔兹

    1499: [NOI2005]瑰丽华尔兹 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 893  Solved: 508[Submit][Status] ...