胜利大逃亡(续)

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

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
 

题解:状态压缩bfs,比赛的时候无限me。。。。心中一万头草泥马奔腾。。。最后大家都说用状态压缩记录钥匙,,,瞬间感觉自己智商负无穷了。。。

其实也不难想,关键以前没怎么用状态压缩;当时想到了用vis存位置以及钥匙数,但是不知道怎么记录,为什么想不到状态压缩呐。。。要长记性了;

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
struct Node{
int nx,ny,step;
//int key[11];
int key;
};
char mp[][];
int vis[][][];
int disx[] = {,,,-};
int disy[] = {,-,,};
int n,m,t;
void bfs(int sx,int sy){
memset(vis,,sizeof(vis));
queue<Node>q;
Node a,b;
a.nx = sx;a.ny = sy;
a.step = ;
//for(int i = 0;i < 11;i++)a.key[i] = 0;
a.key = ;
vis[sx][sy][] = ;
q.push(a);
while(!q.empty()){
a = q.front();
q.pop();
for(int i = ; i < ;i++){
b.nx = a.nx + disx[i];
b.ny = a.ny + disy[i];
b.step = a.step + ;
int x = b.nx, y = b.ny, step = b.step;
//for(int i = 0;i < 11;i++)b.key[i] = a.key[i];
b.key = a.key;
if(x < || y < || x >= n || y >= m)continue;
if(mp[x][y] == '*')continue;
if(step >= t)continue;
if(mp[x][y] == '^'){
printf("%d\n",step);
return;
}
// if(vis[x][y] > 10)continue;
if(mp[x][y] >= 'a' && mp[x][y] <= 'j'){
//b.key[mp[x][y] - 'a'] = 1;
int p = << (mp[x][y] - 'a');
b.key |= p;
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
continue;
}
if(mp[x][y] >= 'A' && mp[x][y] <= 'J'){
//if(b.key[mp[x][y] - 'A'] == 0)continue;
int p = << (mp[x][y] - 'A');
if(b.key & p){
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
}
continue;
}
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
}
}
puts("-1");
return;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&t)){
int sx,sy;
for(int i = ;i < n; i++){
scanf("%s",mp[i]);
for(int j = ;j < m; j++){
if(mp[i][j] == '@'){
sx = i;
sy = j;
}
}
}
mp[sx][sy] = '.';
bfs(sx,sy);
}
return ;
}

胜利大逃亡(续)(状态压缩bfs)的更多相关文章

  1. 胜利大逃亡(续)hdu1429(bfs)

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

  2. HDU 1429 胜利大逃亡(续)(三维BFS)

    题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...

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

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

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

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

  5. hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】

    题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...

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

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

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

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

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

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

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

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

随机推荐

  1. Jquery时间快捷控件(Jtime)配置v1.0

    1.脚本代码行 /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * @date 2013- ...

  2. UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>

    H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others) Subm ...

  3. CodePen&#39;s CSS

    p{text-indent:2em;}前端开发whqet,csdn,王海庆,whqet,前端开发专家 翻译自:CodePen's CSS 翻译人员:前端开发whqet,意译为主.不当之处欢迎大家指正. ...

  4. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  5. linux配置yum源

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  6. python 笔记4-- 函数式编程

    高阶函数 把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 在python中 函数也是一种变量 def add(x, y, f): return f(x) + f( ...

  7. aspnet_regiis 加密/解密 web.config

    加密: @echo off echo web.config c: cd c:\windows\Microsoft.NET\Framework64\v4.0.30319 aspnet_regiis -p ...

  8. javascript模式——Decorator

    Decorator 模式是一种结构型模式,他意在促进代码的复用,是塑造子类的一个方式. 这种想法是基于,新增的属性,对于对象来说不是必须的基本功能.我们为特殊的对象添加自己的方法,而不是重新创建一个类 ...

  9. 在Android Studio中进行单元测试和UI测试

    本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...

  10. APP设计规范大全

    大图可保存到本地