hdu 1885 Key Task (三维bfs)
之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下。。。。
还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到达目标点的路径
就是最小的伤害,因为每一个点的伤害是 不一样的, 这种情况要用优先队列优化, 对伤害优化。
题意:*开始, X出口, b, y, r, g 代表钥匙,分别可以开B, Y, R, G颜色的门, 钥匙可以多次使用。问最短的步骤。
思路:vis[][]【】数组开三维,第三维记录状态 是否拿到该颜色的钥匙, 如果以相同的状态走到 相同的地方 就是重复,不能加进队列。
第三维用 二进制下相应的位来表示 是否拿到相应的钥匙。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = +;
int vis[maxn][maxn][], r, c;
char G[maxn][maxn];
int dx[] = {,,,-};
int dy[] = {,-,,};
char k[] = {'b', 'y', 'r', 'g'};
char d[] = {'B', 'Y', 'R', 'G'};
struct node
{
int x, y, key, step;
} next, pos; void bfs(int a, int b)
{
int i, j;
queue<node>q;
memset(vis, , sizeof(vis));
next.x = a;
next.y = b;
next.key = ;
next.step = ;
q.push(next);
vis[a][b][next.key] = ;
while(!q.empty())
{
pos = q.front();
q.pop();
for(i = ; i < ; i++)
{
next.x = pos.x+dx[i];
next.y = pos.y+dy[i];
next.step = pos.step+;
if(!(next.x>=&&next.x<=r&&next.y>=&&next.y<=c))
continue;
if(G[pos.x][pos.y]=='X')
{
printf("Escape possible in %d steps.\n", pos.step);
return;
}
if(G[next.x][next.y]=='#')
continue;
if(G[next.x][next.y]>='a' && G[next.x][next.y]<='z')
{
for(j = ; j < ; j++)
if(G[next.x][next.y]==k[j])
next.key = (pos.key|(<<j));
}
if(G[next.x][next.y]=='.'||G[next.x][next.y]=='*')
{
next.key = pos.key;
}
if(G[next.x][next.y]>='A' && G[next.x][next.y]<='Z')
{
for(j = ; j < ; j++)
if(G[next.x][next.y]==d[j])
{
if(pos.key&(<<j))
next.key = pos.key;
else
break;
}
if(j<)
continue;
}
if(vis[next.x][next.y][next.key]==)
{
vis[next.x][next.y][next.key] = ;
q.push(next);
}
}
}
printf("The poor student is trapped!\n");
}
int main()
{
int i, j;
int a, b;
while(cin>>r>>c)
{
if(r== && c==) break;
for(i = ; i <= r; i++)
{
getchar();
for(j = ; j <= c; j++)
{
cin>>G[i][j];
if(G[i][j]=='*')
{
a = i;
b = j;
}
}
}
bfs(a, b);
}
return ;
}
hdu 1885 Key Task (三维bfs)的更多相关文章
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1885 Key Task
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1885 Key Task(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1885 再贴一个链接http://blog.csdn.net/u013081425/article/details ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- IIS7.5 HTTP 错误 500.19 - Internal Server Error 问题的解决方案
昨天在 windows 7 下用 IIS 7.5 运行一个以前用 .NET Framework 3.5 写的项目,发现总是出现 500.19 错误,如下: 百度了好久,没找到解决问题确切的答案,我也知 ...
- PAT-乙级-1055. 集体照 (25)
1055. 集体照 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 拍集体照时队形很重要,这里对给定的N ...
- Unity3D研究院之IOS本地消息通知LocalNotification的使用
原地址:http://www.xuanyusong.com/archives/2632 现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器 ...
- Javascript 中childNodes和children的区别
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- javascript背景淡入淡出
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 简单易懂的现代魔法——Play Framework攻略2
接前文:http://www.cnblogs.com/Kassadin/p/4335908.html 上次讲到Play Framework开发环境的配置,以及第一个Hello World程序:本次主要 ...
- IIS常见错误及解决
IIS常见错误 1.HTTP 错误 404.3 - Not Found由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决办法: w ...
- aircrack-ng 字典破解WPA / WPA2
1. 安装 首先安装两个扩展sudo apt-get install build-essentialsudo apt-get install libssl-dev 然后到http://download ...
- 随机森林分类(Random Forest Classification)
其实,之前就接触过随机森林,但仅仅是用来做分类和回归.最近,因为要实现一个idea,想到用随机森林做ensemble learning才具体的来看其理论知识.随机森林主要是用到决策树的理论,也就是用决 ...
- mysql23个知识点
1.它是一种解释语言:写一句执行一句,不需要整体编译执行. 2.1.没有“ ”,字符串使用‘ '包含 3.一个表只有一个主键,但是一个主键可以是由多个字段组成的 组合键 4.实体完整性:实体就是指一条 ...