胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8379    Accepted Submission(s): 3008

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
Author
LL
Source
这个看了别人的代码  发现还可以这样写
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#include<stack>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
char mp[N][N];
int _x[]={,,,-};
int _y[]={,,-,};
int vis[][][];
int pow2[];
int n,m,t;
struct node{
int x,y;
int step,k;
};
void init()
{
pow2[]=;
for(int i=;i<=;i++)
pow2[i]=<<i;
}
bool judge(int x,int y,int k)
{
if(x<||y<||x>=n||y>=m||mp[x][y]=='*')
return false;
if(vis[x][y][k])return false;
return true;
}
int BFS(int x,int y)
{
queue<node>q;
node first,next;
first.x=x;first.y=y;
first.step=first.k=;
q.push(first);
vis[x][y][]=;
while(!q.empty()) {
//cout<<2<<endl;
next=q.front();
q.pop();
for(int i=;i<;i++) {
int xx=next.x+_x[i];
int yy=next.y+_y[i];
if(judge(xx,yy,next.k)){
if(mp[xx][yy]=='^')
return next.step+;
first.x=xx;
first.y=yy;
first.step=next.step;
first.k=next.k;
if(mp[xx][yy]>='A'&&mp[xx][yy]<='J') {
int c=mp[xx][yy]-'A';
if(first.k&pow2[c]){
vis[xx][yy][first.k]=;
first.step+=;
q.push(first);
}
}
else if(mp[xx][yy]>='a'&&mp[xx][yy]<='j') {
int c = mp[xx][yy]-'a';
if(!vis[xx][yy][first.k|pow2[c]]){
first.k=first.k|pow2[c];
vis[xx][yy][first.k]=;
first.step+=;
q.push(first);
}
}
else if(mp[xx][yy]=='.'||mp[xx][yy]=='@') {
vis[xx][yy][first.k]=;
first.step+=;
q.push(first);
}
}
}
}
return -;
}
int main(){
//getchar();
init();
while(scanf("%d%d%d",&n,&m,&t)!=EOF){
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
scanf("%s",mp[i]);
int x,y;
for(int i=;i<n;i++)
for(int j=;j<m;j++) {
if(mp[i][j]=='@'){
x=i;
y=j;
}
}
int ans=BFS(x,y);
if(ans==-||ans>= t)
cout<<-<<endl;
else
cout<<ans<<endl;
}
}

hdu 1429(BFS+状态压缩)的更多相关文章

  1. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到 ...

  3. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  4. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  5. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  6. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  9. BFS+状态压缩 HDU1429

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

  10. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. 移动web——bootstrap响应式工具

    基本介绍 1.利用媒体查询功能并使用这些工具类可以方便的针对不同设备展示或隐藏页面内容. 基本使用 <!DOCTYPE html> <html lang="zh-CN&qu ...

  2. JS——回调函数

    1.回调函数作为参数的形式进行使用 2.回调函数一定程度上达到了解耦效果(模块化.功能化) <script> console.log(op(1, 2, add)); console.log ...

  3. 认识 java JVM虚拟机选项 Xms Xmx PermSize MaxPermSize 区别

    点击window---->preferences---->配置的tomcat---->JDK,在Optional Java VM arguments:中输入 -Xmx512M -Xm ...

  4. 如何在Android Studio中查看一个类的继承关系呢?

    在面板顶部的工具栏中,找到Navigate,然后在下拉列表中,找到“Type Hierarchy”(快捷键 Ctrl+H),点击.即可在面板右侧出现该类的Hierarchy层级图.

  5. docker和jenkins安装启动

    docker安装Jenkins 1.安装Docker 1.1 yum 包更新到最新 sudo yum update 1.2 安装需要的软件包, yum-util 提供yum-config-manage ...

  6. 10.mysql事务管理及python操作

    在用户操作MySQL过程中,对于一般简单的业务逻辑或中小型程序而言,无需考虑应用MySQL事务.但在比较复杂的情况下,往往用户在执行某些数据操作过程中,需要通过一组SQL语句执行多项并行业务逻辑或程序 ...

  7. 【模板】dijkstra

    洛谷 4779 #include<cstdio> #include<cstring> #include<algorithm> #include<queue&g ...

  8. hdu 5178 pairs

    pairs 问题描述 John 在X轴上拥有nn个点,他们的坐标分别为$(x[i],0),(i=0,1,2,…,n-1)$. 他想知道有多少对< a,b ><a,b>满足|x[ ...

  9. Vue 安装教程

    1.下载node.js https://nodejs.org/en/ 2.检查环境变量: npm init (初始化项目) npm i webpack vue vue-loader 安装依赖: npm ...

  10. 曾经遇过的sql问题

    曾经遇过的sql问题 问题一: 语句1: select SUM(level) from Comment 语句2: ELSE SUM(level) END as totalLevel from Comm ...