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> ...
随机推荐
- mouseover事件mouseenter事件
1. mouseover:会在鼠标进入该元素或者该元素的子元素时触发 mouseenter:只在鼠标进入该元素时触发 mouseout:在鼠标移出该元素或则该元素的子元素时触发 mouseleave: ...
- Pandas常用函数入门
一.Pandas Python Data Analysis Library或Pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的.Pandas纳入了大量库和一些标准的数据模型, ...
- freeMarker遍历map的正确方式
假设selectDateModel 是我们后台返回的map<String, String>; <#list selectDateModel?keys as key> <o ...
- jQuery DataTables 获取选中行数据
如题 想获取操作 DataTables 获取选中行数据 案1.主要是利用 js getElementsByTagName 函数 然后对获取到的tr 进行操作 如下 function getChec ...
- .Net开发小技巧
.NET项目开发中的小技巧 1.不要频繁的创建对象...这个损失是巨大的...new太多了,后果比较严重. 2.打开数据库后要及时的关闭连接,如果你不能做到一个open后跟一个close,那也没关系, ...
- 10个鲜为人知的C#关键字
在正式开始之前,我需要先声明:这些关键字对于偏向底层的程序员更加耳熟能详,对这些关键字不了解并不影响你作为一个合格的程序员. 这意味着这些关键字会让你在编写程序时得到更好的代码质量和可读性,enjoy ...
- 百度统计的JS脚本原理分析
首先,百度统计会要求我们在要统计的页面中嵌入一段js语句,类似如下: <script type="text/javascript">var _bdhmProtocol ...
- 使用.net core在Ubuntu构建一个TCP服务器
介绍和背景 TCP编程是网络编程领域最有趣的部分之一.在Ubuntu环境中,我喜欢使用.NET Core进行TCP编程,并使用本机Ubuntu脚本与TCP服务器进行通信.以前,我在.NET框架本身写了 ...
- TweenMax动画库学习
之前在做HTML5移动端开发的时候,用的都是Animate.css,这个插件封装的的确很好,但是在做一些缓动方面的动画,它也有一定的不足之处,比如手要写一个连续的动画,需要不停的去重复写函数,使得代码 ...
- 如何从零开始对接第三方登录(Java版):QQ登录和微博登录
前言 个人网站最近增加了评论功能,为了方便用户不用注册就可以评论,对接了QQ和微博这2大常用软件的一键登录,总的来说其实都挺简单的,可能会有一点小坑,但不算多,完整记录下来方便后来人快速对接. 后台设 ...