BNUOJ 5629 胜利大逃亡(续)
胜利大逃亡(续)
This problem will be judged on HDU. Original ID: 1429
64-bit integer IO format: %I64d Java class name: Main
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
Input
. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
Output
Sample Input
4 5 17
@A.B.
a*.*.
*..*^
c..b* 4 5 16
@A.B.
a*.*.
*..*^
c..b*
Sample Output
16
-1
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
struct node{
int state,step;
node(int x = ,int y = ):state(x),step(y){}
};
char mp[][];
int n,m,t,sx,sy,ex,ey;
const int dir[][] = {,-,,,-,,,};
queue<node>q;
set<int>s;
int compress(int x,int y,int state){
int temp = x;
temp = (temp<<)|y;
temp = (temp<<)|state;
return temp;
}
void decompress(int temp,int &x,int &y,int &state){
int t = (<<)-;
state = temp&t;
temp >>= ;
t = (<<)-;
y = temp&t;
temp >>= ;
x = temp&t;
}
void addkey(int &state,int t){
state |= (<<t);
}
int haskey(int state,int t){
return state&(<<t);
}
int bfs(){
while(!q.empty()) q.pop();
s.clear();
int i,j,x,y,tx,ty,tmpstate,tmp;
tmp = compress(sx,sy,);
node temp2 = node(tmp,);
q.push(temp2);
s.insert(tmp);
while(!q.empty()){
node temp2 = q.front();
q.pop();
if(temp2.step > t) return INF;//必须要的优化
for(i = ; i < ; i++){
decompress(temp2.state,x,y,tmpstate);
if(x == ex && y == ey) return temp2.step;
tx = x+dir[i][];
ty = y+dir[i][];
if(mp[tx][ty] == '*') continue;
if(mp[tx][ty] >= 'a' && mp[tx][ty] <= 'j'){
addkey(tmpstate,mp[tx][ty]-'a');
}else if(mp[tx][ty] >= 'A' && mp[tx][ty] <= 'J'){
if(!haskey(tmpstate,mp[tx][ty]-'A')) continue;
}
tmp = compress(tx,ty,tmpstate);
if(s.count(tmp)) continue;
s.insert(tmp);
q.push(node(tmp,temp2.step+));
}
}
return INF;
}
int main(){
while(~scanf("%d %d %d",&n,&m,&t)){
memset(mp,'*',sizeof(mp));
getchar();
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
mp[i][j] = getchar();
if(mp[i][j] == '@') {sx = i;sy = j;}
if(mp[i][j] == '^') {ex = i;ey = j;}
}
getchar();
}
int tmp = bfs();
printf("%d\n",tmp < t?tmp:-);
}
return ;
}
BNUOJ 5629 胜利大逃亡(续)的更多相关文章
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- HDOJ 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- 胜利大逃亡(续)(状态压缩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 Subm ...
- 胜利大逃亡(续)hdu1429(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Volley的初步了解
Volley的介绍 Volley是什么? 2013年Google I/O大会上推出的网络请求和图片加载框架 其优点是api简单,性能优秀 非常适合数据量不大但是通信频繁的网络请求,而对于大数据量的操作 ...
- (四)SpringIoc之Bean装配
在pom.xml的依赖 <dependencies> <!--测试包--> <dependency> <groupId>junit</groupI ...
- (二)Spring容器
大佬总结的很好,请去看大佬博客. http://www.cnblogs.com/chenssy/archive/2012/11/15/2772287.html https://www.cnblogs. ...
- swiper4实现的拥有header和footer的全屏滚动demo/swiper fullpage footer
用swiper4实现的拥有header和footer的全屏滚动demo, <!DOCTYPE html> <html lang="en"> <head ...
- java jar文件打包成exe(Launch4j使用说明)
在日常的项目中需要把jar打包成exe.怎样快速的实现此功能.下面通过Launch4j的使用方法来介绍整个打包过程. 第一步:生成jar文件 第二部:使用Launch4j 图来描述过,简单明了.一切尽 ...
- phpmyadmin在linux下通过sock安装教程
当初是按照 http://www.cnblogs.com/freeweb/p/5262852.html 地址参考安装,因为疏忽,未考虑到版本差异带来的影响(自身安装的是最新版 phpMyAdmin-4 ...
- Shiro的subject实质上是当前执行用户的特定视图。
Shiro的subject实质上是当前执行用户的特定视图. 通过org.apache.shiro.SecurityUtils可以查询当前执行用户: Subject currentUser = Secu ...
- MFC_VS清理器
VS清理器 界面 工程目录 列表控件ID改名IDC_FILELIST 绑定变量m_FileList 属性设置Accept Files 设置True 成员添加 // 用于保存待遍历的目录 vector& ...
- CREATE VIEW - 定义一个视图
SYNOPSIS CREATE [ OR REPLACE ] VIEW name [ ( column_name [, ...] ) ] AS query DESCRIPTION 描述 CREATE ...
- 了解Java密码扩展的基础
了解Java密码扩展的基础 Java密码扩展(The Java Cryptography Extension),是JDK1.4的一个重要部分,基本上,他是由一些包构成的,这些包形成了一个框 ...