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> ...
随机推荐
- body.clientHeight与documentElement.clientHeight
body上的clientHeight会对着内容区域的高度变化而变化,当内容只有100px,则body上只有100px被撑起,返回的clientHeight为100: documentElement.c ...
- 解决mysql启动失败报1067错误
最近做项目使用 mysql 数据库 ,因为卸载了鲁大师造成了数据库文件缺失.重装mysql数据库后启动出现了1067错误,详情如下 在网上查了错误原因,将my.ini文件下的默认搜索引擎换成了 myi ...
- HTTP协议相关知识点
主要参考 http://www.imooc.com/article/14397,来源:慕课网,作者种子_fe HTTP是超文本传输协议,主要特点有: 支持客户.服务器模式 简单快速:客户向服务器请求服 ...
- Python-psutil模块
psutil 1.简单介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等) ...
- 获取所有栈的信息,只有最上面的和最下面的,但是不能获取栈中间的activity信息
直接在cmd窗口上输入 adb shell后,再输入dumpsys activity activities,可以看到所有的activity以及相关栈状态
- MacOS 下安装mysqlclient 的问题及解决办法
[操作环境] 操作系统:MacOS X 10.13.1 mysql运行环境:Docker Docker版本:17.09-ce 在开发Django时,刚开始使用的sqlite进行开发,想部署到生产环境需 ...
- 三种读取HashMap的方式
package com.biubiu.entity; import java.util.Collection; import java.util.HashMap; import java.util.I ...
- 【朝花夕拾】朝花夕拾-Robot Framework实战演练之开篇
(原创文章,转载请注明出处.) 开博了,简单感慨两句. 前些年一直在做质量体系建设及团队管理的事,忽略了对测试技术热度的保持,这两年有幸重回开发测试第一线,颇感欣喜. 近期随着公司新业务的开展,需要快 ...
- [验证码识别技术]字符验证码杀手--CNN
字符验证码杀手--CNN 1 abstract 目前随着深度学习,越来越蓬勃的发展,在图像识别和语音识别中也表现出了强大的生产力.对于普通的深度学习爱好者来说,一上来就去跑那边公开的大型数据库,比如I ...
- 将下载到本地的JAR包手动添加到Maven仓库
<!-- https://mvnrepository.com/artifact/ojdbc/ojdbc --><!-- (参数一):下载到本地的ojdbc-10.2.0.4.0.ja ...