胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6656    Accepted Submission(s): 2315

Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……



这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门。钥匙藏在地牢另外的某些地方。刚開始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟仅仅能从一个坐标走到相邻四个坐标中的当中一个。

魔王每t分钟回地牢视察一次。若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。如今请你帮他计算是否能再次成功逃亡。仅仅要在魔王下次视察之前走到出口就算离开地牢,假设魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。

 
Input
每组測试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图。当中包含:



. 代表路

* 代表墙

@ 代表Ignatius的起始位置

^ 代表地牢的出口

A-J 代表带锁的门,相应的钥匙分别为a-j

a-j 代表钥匙,相应的门分别为A-J



每组測试数据之间有一个空行。
 
Output
针对每组測试数据。假设能够成功逃亡。请输出须要多少分钟才干离开,假设不能则输出-1。

 
Sample Input
4 5 17
@A.B.
a*.*.
*..*^
c..b* 4 5 16
@A.B.
a*.*.
*..*^
c..b*
 
Sample Output
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 &amp;&amp; 状态压缩】的更多相关文章

  1. hdu - 1429 胜利大逃亡(续) (bfs状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...

  2. hdu 1429 胜利大逃亡(续) (bfs+状态压缩)

    又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...

  3. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...

  5. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

  7. hdu 1429 胜利大逃亡(续)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...

  8. HDU 1429 胜利大逃亡(续)(DP + 状态压缩)

    胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...

  9. HDU 1429 胜利大逃亡(续)(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. python的enumerate()函数

    其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list):       ...

  2. VS2010中文注释带红色下划线的解决方法

    环境:Visual Studio 2010 问题:代码中出现中文后会带下划线,很多时候感觉很不舒服.找了很久的原因没找到,后来无意中在VisualAssist X里找到了解决办法. 1.安装完Visu ...

  3. flume+flume+kafka消息传递+storm消费

    通过flume收集其他机器上flume的监测数据,发送到本机的kafka进行消费. 环境:slave中安装flume,master中安装flume+kafka(这里用两台虚拟机,也可以用三台以上) m ...

  4. 【K8s】Kubernetes架构理解

    抽空学习了一下Kubernetes,感觉和大数据领域内集群的资源管理.任务调度等有异曲同工之处,简单总结一下备忘. [概念] Kubernetes是一个工业级的容器编排平台,单词有点长,常用K8s代称 ...

  5. Redis(四)-配置

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法 Redis CONFIG 命令格式如下: ...

  6. MyEclipse中快速复制粘贴当前行的操作

  7. Spring 整合 Redis (零配置) 的简单使用

    pom.xml <!--jedis--> <dependency> <groupId>redis.clients</groupId> <artif ...

  8. 【Oracle】详解ADDM工具

    一.ADDM简介           在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set event 10046& ...

  9. Yoga710笔记本Win10和Ubuntu系统共存

    联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...

  10. An interesting scroll background------ActionScript3.0

    package { /* *@ ClassName : package::backGround *@ INTRO : the continuously scroll background *@ Aut ...