hdu 1885 Key Task(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1885
再贴一个链接http://blog.csdn.net/u013081425/article/details/21756237
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#define LL long long
#define _LL __int64 using namespace std;
const int INF = 0x3f3f3f3f; int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
int x,y;
int sta;
int step;
}; int n,m;
int sx,sy;
char map[110][110];
int mark[110][110][(1<<4)+10]; queue <struct node>que; int ans; int small(char ch)
{
if(ch == 'b')
return 0;
else if(ch == 'y')
return 1;
else if(ch == 'r')
return 2;
else if(ch == 'g')
return 3;
} int big(char ch)
{
if(ch == 'B')
return 0;
else if(ch == 'Y')
return 1;
else if(ch == 'R')
return 2;
else if(ch == 'G')
return 3;
} void bfs()
{
while(!que.empty()) que.pop();
memset(mark,0,sizeof(mark)); mark[sx][sy][0] = 1;
que.push((struct node) {sx,sy,0,0}); while(!que.empty())
{
struct node u = que.front();
que.pop();
if(map[u.x][u.y] == 'X')
{
ans = u.step;
return;
} for(int d = 0; d < 4; d++)
{
int x = u.x + dir[d][0];
int y = u.y + dir[d][1];
int sta = u.sta; if(x < 1 || x > n || y < 1 || y > m) continue; if(map[x][y] == '.' || map[x][y] == '*' || map[x][y] == 'X') //注意出口‘X’也要进队列
{
if(!mark[x][y][sta])
{
mark[x][y][sta] = 1;
que.push((struct node){x,y,sta,u.step+1});
}
}
else if(map[x][y] >= 'a' && map[x][y] <= 'z')
{
int add = small(map[x][y]); if((sta&(1<<add)) == 0)
sta += (1 << add);
if(!mark[x][y][sta])
{
mark[x][y][sta] = 1;
que.push((struct node){x,y,sta,u.step+1});
}
}
else if(map[x][y] >= 'A' && map[x][y] <= 'Z')
{
int add = big(map[x][y]);
if( (sta&(1<<add)) && !mark[x][y][sta] )
{
mark[x][y][sta] = 1;
que.push((struct node){x,y,sta,u.step+1});
}
}
}
}
} int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n == 0 && m == 0) break;
for(int i = 1; i <= n; i++)
{
scanf("%s",map[i]+1);
for(int j = 1; j <= m; j++)
{
if(map[i][j] == '*')
{
sx = i;
sy = j;
}
}
} ans = -1;
bfs();
if(ans == -1)
printf("The poor student is trapped!\n");
else printf("Escape possible in %d steps.\n",ans);
}
return 0;
}
hdu 1885 Key Task(bfs)的更多相关文章
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- 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 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- 【HDOJ】1885 Key Task
状态压缩+BFS,一次AC. /* 1885 */ #include <iostream> #include <queue> #include <cstring> ...
随机推荐
- Ubuntu16 远程连接MySQL
1.进入MySQL配置目录允许其他IP可以链接 vi /etc/mysql/mysql.conf.d/mysqld.cnf 吧下面这行注释掉 #bind-address = 127.0.0.1 2.远 ...
- react-native多图选择、图片裁剪(支持ad/ios图片个数控制)
扯淡: 目前关于rn比较知名并且封装好的图片选择控件很多,不过能同时支持多图片上传,个数控制兼容iOS/Ad的却寥寥无几,而今天介绍的这款框架可以实现:图片裁剪.最大图片个数限制.拍照.本地相册等功能 ...
- Mybatis面试整理
#{}和${}的区别 #{}是预编译处理,${}是字符串替换. Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值: Mybatis ...
- 对比MFC和Winform及WPF
MFC 生成本机代码,自然是很快.可是,消息循环,减缓了界面显示速度.winform 封装了 win32 的api,多次进行P/invoke 操作 (大部分使用p/invoke操作封装),速度慢 .w ...
- 六:Ioc和AOP使用拓展
Ioc和AOP使用拓展 一:1.构造注入 一个<constructor-arg>元素表示构造方法的一个参数,且使用时不区分顺序,index指定元素,位置从0开始,Type用来指定参数,避免 ...
- python基础阶段 经典练习题 拾英札记(3)
对于编程学习来说,动手操练和重复训练很重要. 因为这是一个注重实践的活,最终要下笔落字. 更何况,即使你看了很多博客,听了很多课,你脑中的认识和手指下的-屏幕上的反馈,逻辑上是两个维度-两个载体的,中 ...
- [转载] Tomcat架构分析
转载自http://gearever.iteye.com/category/223001
- 分布式网络文件系统--MooseFS
一.介绍 1.简介 MooseFS是一个具备冗余容错功能的分布式网络文件系统,它将数据分别存放在多个物理服务器或单独磁盘或分区上,确保一份数据有多个备份副本.对于访问的客户端或者用户来说,整个分布式网 ...
- .net SignalR winform 推送广播
最近在做一个项目,需要用到服务端主动推送给客户端,最开始用的是自己比较顺手的Remoting,可后来发现把服务端架到外网上,就猴子它哥了,后来又尝试WCF,虽然能推送,但是推了几次也猴子它哥了,后来找 ...
- mysql数据库常用命令笔记
连接数据库:mysql -h localhost -u root -p 000000 退出:exit; \q; quit; SET foreign_key_checks = 0; 禁用外键 ...