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 ...
随机推荐
- 【转载】使用Axure制作App原型怎样设置尺寸?
使用Axure制作App原型怎样设置尺寸? 原文地址:http://www.axure.us/2172/ 本文由原型库网站投稿,转载请注明出处. 最近有几位小伙伴儿都提出同样一个疑问:想用Axure设 ...
- Notes of the scrum meeting(11/1)
meeting time:9:00~10:30p.m.,November 1st,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- (转)assert()函数用法总结
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h>void assert( in ...
- 学习KnockOut第三篇之List
学习KnockOut第三篇之List 欲看此篇---------------------------------------------可先看上篇. 第一步,先搭建一个大概的框架起来 ...
- [设计模式] 19 观察者模式 Observer Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对观察者模式是这样说的:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.当一个 ...
- 国产神通数据库操作备忘(Linux)
最近接触到国产神通数据库的一个项目,发现这个数据库还是挺有技术含量的,看起来做的还不错. 1.启动停止 在终端窗口中输入以下命令启动数据库: # /etc/init.d/oscardb_<数据库 ...
- this指针指向的彻底理解
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- substr_replace()函数:将手机号中间4位隐藏为*号
<?php $mobile = "15810320826"; echo substr_replace($mobile,'****',3 , 4); ?> substr_ ...
- Android Service 的一些笔记
绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...
- hdu1017
http://acm.hdu.edu.cn/showproblem.php?pid=1017 #include<iostream> #include<stdio.h> #inc ...