HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6656 Accepted Submission(s): 2315
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门。钥匙藏在地牢另外的某些地方。刚開始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟仅仅能从一个坐标走到相邻四个坐标中的当中一个。
魔王每t分钟回地牢视察一次。若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。如今请你帮他计算是否能再次成功逃亡。仅仅要在魔王下次视察之前走到出口就算离开地牢,假设魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,相应的钥匙分别为a-j
a-j 代表钥匙,相应的门分别为A-J
每组測试数据之间有一个空行。
4 5 17
@A.B.
a*.*.
*..*^
c..b* 4 5 16
@A.B.
a*.*.
*..*^
c..b*
16
-1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 25
using namespace std; int vis[maxn][maxn][1 << 11];
char map[maxn][maxn];
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; struct node{
int x, y ,step, key;
}; int n, m, t, sx, sy; bool check(node a){
if(a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && map[a.x][a.y] != '*')
return true;
else
return false;
} int bfs(){
node now, next;
queue<node >q;
now.x = sx;
now.y = sy;
now.step = 0;
now.key = 0;
vis[now.x][now.y][now.key] = true;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
if(map[now.x][now.y] == '^' && now.step < t){
return now.step;
}
if(now.step > t) continue;
for(int i = 0; i < 4; ++i){
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.step = now.step + 1;
if(check(next)){
if(map[next.x][next.y] >= 'a' && map[next.x][next.y] <= 'z'){//钥匙
next.key = now.key | (1 << (map[next.x][next.y] - 'a'));//获得这个钥匙
if(!vis[next.x][next.y][next.key]){
vis[next.x][next.y][next.key] = 1;
q.push(next);
}
}
else if(map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'Z'){//门
next.key = now.key;
if(next.key & (1 << (map[next.x][next.y] - 'A'))){//拥有这个门的钥匙
if(!vis[next.x][next.y][next.key]){
vis[next.x][next.y][next.key] = 1;
q.push(next);
}
}
}
else {//路
next.key = now.key;
if(!vis[next.x][next.y][next.key]){
vis[next.x][next.y][next.key] = 1;
q.push(next);
}
}
}
}
}
return -1;
} int main (){
while(scanf("%d%d%d", &n, &m, &t) != EOF){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++i){
scanf("%s", map[i]);
for(int j = 0; j < m; ++j)
if(map[i][j] == '@')
sx = i, sy = j;
}
int ans;
ans = bfs();
printf("%d\n", ans);
}
return 0;
}
HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】的更多相关文章
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- HDU 1429 胜利大逃亡(续)(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖
题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...
- POI合并单元边框问题解决方法
http://blog.csdn.net/hardworking0323/article/details/51105430
- lnmp 安装FTP服务 并配置FTP用户
lnmp 默认是不带FTP服务的,需要的童鞋要自行安装.步骤也很简单 一,进入lnmp目录,找到pureftpd.sh 二,直接运行该脚本 ./pureftpd.sh 按任意键开始安装,等待,安装成功 ...
- 5.12redis
Window配置Redis环境和简单使用 一.关于Redis Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序 ...
- EasyUI 编写实体类树状选择器
<%@ page contentType="text/html;charset=UTF-8"%> <%@ include file="/WEB-INF/ ...
- checked、disabled在原生、jquery、vue下不同写法
以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...
- 【PostgreSQL-9.6.3】log参数的设置
编辑数据目录中的postgresql.conf参数文件,我的数据目录是/usr/local/pgsql/data vi postgresql.conf 找到如下内容: ... #----------- ...
- 【PLSQL】游标
Oracle中的SQL在执行时需要分配一块内存区域,这块内存区域叫做上下文区. 上下文区中记录了SQL语句的处理信息,这些信息包括:查询返回的数据行.查询所处理的数据的行号.指向共享池中的已分析的SQ ...
- 25-Ubuntu-文件和目录命令-其他命令-重定向
重定向 Linux允许将命令执行结果重定向到一个文件. 将本应显示到终端上的内容输出或追加到指定文件中. 重定向命令 含义 > 表示输出,会覆盖原有文件. >> 表示追加,会将内容追 ...
- 11.5 【Linq 】连接
11.5.1 使用 join 子句的内连接 如果你打算把一个巨大的序列连接到一个极小的序列上,应尽可能把小序列作为右边序列 class Program { static void Main(strin ...