hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6070 Accepted Submission(s): 2096
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
. 代表路 * 代表墙 @ 代表Ignatius的起始位置 ^ 代表地牢的出口 A-J 代表带锁的门,对应的钥匙分别为a-j a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
-1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
typedef long long ll ;
const int M = + , mod = , bas = ;
char map[M][M] ;
int a[M][M][ + ] ;
int move[][] = {{,} , {-,} , {,} , {,-}} ;
int n , m , t ;
struct node
{
int x , y , step ;
int key ;
}; int bfs (int sx , int sy , int ex , int ey)
{
// puts ("heheda") ;
std::queue<node> q ;
while (!q.empty ()) q.pop () ;
node ans , tmp ;
q.push ((node) {sx , sy , ,}) ;
a[sx][sy][] = ;
while (!q.empty ()) {
ans = q.front () ; q.pop () ;
// puts ("He") ;
// printf ("s----------(%d,%d),%d\n" , (ans.x , ans.y) , ans.step) ;
if (ans.step >= t) return - ;
if (ans.x == ex &&ans.y == ey ) return a[ans.x][ans.y][ans.key];
for (int i = ; i < ; i ++) {
tmp.x = ans.x + move[i][] ; tmp.y = ans.y + move[i][] ;
if (tmp.x < || tmp.y < || tmp.x >= n || tmp.y >= m) continue ;
if (map[tmp.x][tmp.y] == '*') continue ;
char door = map[tmp.x][tmp.y] ;
if (door >= 'A' && door <= 'J' ) if ( ! (ans.key & (int)pow(,door - 'A') ) ) continue ;
tmp.step = ans.step + ;
tmp.key = ans.key ;
if (door >= 'a' && door <= 'j' ) tmp.key = tmp.key | ((int) pow ( , door - 'a')) ;
if (a[tmp.x][tmp.y][tmp.key] != - ) continue ;
a[tmp.x][tmp.y][tmp.key] = tmp.step ;
q.push (tmp) ;
}
}
return - ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin );
while (scanf ("%d%d%d" , &n , &m , &t) == ) {
int x , y , ex , ey ;
// printf ("n=%d,m=%d,t=%d\n" , n , m , t ) ;
memset (a , - , sizeof(a)) ;
for (int i = ; i < n ; i ++) scanf ("%s" , map[i]) ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
if (map[i][j] == '@') {x = i , y = j ;}
else if (map[i][j] == '^') {ex = i , ey = j ;} ;
}
}
printf ("%d\n" , bfs (x , y , ex , ey) ) ;
}
return ;
}
/*
#include<stdio.h>
#include<string.h>
#include<queue>
typedef long long ll ;
const int M = 20 + 4 , mod = 1700003 , bas = 29 ;
char map[M][M] ;
int move[][2] = {{1,0} , {-1,0} , {0,1} , {0,-1}} ;
int n , m , t ;
struct node
{
int x , y , step ;
bool key[30] ;
};
struct hash
{
int w , nxt ;
int x , y , key ;
}e[mod];
int E = 0 , H[mod]; void insert (int x)
{
int y = x % mod ;
if (y < 0) y += mod ;
e[++ E].w = x ;
e[E].nxt = H[y] ;
H[y] = E ;
} bool find (int x , node tmp )
{
int y = x % mod ;
if (y < 0) y += mod ;
for (int i = H[y] ; i ; i = e[i].nxt) {
if (e[i].w == x) {
if (e[i].x == tmp.x && e[i].y == tmp.y && e[i].key)
}
}
return false ;
} int bfs (int sx , int sy , int ex , int ey)
{
std::queue<node> q ;
while (!q.empty ()) q.pop () ;
node ans , tmp ;
q.push ((node) {sx , sy , 0}) ;
while (!q.empty ()) {
ans = q.front () ; q.pop () ;
if (ans.step >= t) return -1 ;
if (ans.x == ex && ans.y == ey ) return ans.step ;
for (int i = 0 ; i < 4 ; i ++) {
tmp.x = ans.x + move[i][0] ; tmp.y = ans.y + move[i][1] ;
if (tmp.x < 0 || tmp.y < 0 || tmp.x >= n || tmp.y >= m) continue ;
if (map[tmp.x][tmp.y] == '*') continue ;
char door = map[tmp.x][tmp.y] ;
if (door >= 'A' && door <= 'J' && ans.key[door - 'A' ] == 0) continue ;
for (int j = 0 ; j < 26 ; j ++) {
tmp.key[j] = ans.key[j] ;
}
tmp.step = ans.step + 1 ;
if (door >= 'a' && door <= 'j') tmp.key[door - 'a' ] = 1 ;
ll rhs = 1 , fac = 1;
rhs = (1ll*rhs*fac + tmp.x) % mod ; fac *= 1ll*bas ; rhs = (1ll*rhs*fac+tmp.y) % mod ; fac *= 1ll*bas ;
for (int i = 0 ; i < 26 ; i ++) {
if (tmp.key[i]) {
rhs = (1ll*rhs*fac + i * i *i + i *i) % mod ;
fac *= 1ll * bas ;
}
}
if (find (rhs , tmp)) continue ;
insert (rhs , tmp) ;
q.push (tmp) ;
}
}
return -1 ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin );
while (~ scanf ("%d%d%d" , &n , &m , &t) ) {
int x , y , ex , ey ;
for (int i = 0 ; i < n ; i ++) scanf ("%s" , map[i]) ;
for (int i = 0 ; i < n ; i ++) {
for (int j = 0 ; j < m ; j ++) {
if (map[i][j] == '@') {x = i , y = j ;}
else if (map[i][j] == '^') {ex = i , ey = j ;} ;
}
}
E = 0 ;
memset (H, 0 , sizeof(H)) ;
printf ("%d\n" , bfs (x , y , ex , ey) ) ;
}
return 0 ;
}
*/
一开始把x , y , key,当做hash的元素,得到一个hash值。
一直wa,后来杰神说,要更加精确的hash值,就是多加点辅助值。
so明白了,不过果然变得很麻烦,给过还是。。。。。
附上用二进制保存钥匙状态的程序。
hdu.1429.胜利大逃亡(续)(bfs + 0101011110)的更多相关文章
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 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+状态压缩,很经典)
传送门: 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 胜利大逃亡(续)(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 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 ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带 ...
随机推荐
- C++ 之const Member Functions
Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...
- AngularJs ngChange、ngChecked、ngClick、ngDblclick
ngChange 当用户更改输入时,执行给定的表达式.表达式是立即进行执行的,这个和javascript的onChange事件的只有在触发事件的变化结束的时候执行不同. 格式:ng-change=”v ...
- CF 444C DZY Loves Physics(图论结论题)
题目链接: 传送门 DZY Loves Chemistry time limit per test1 second memory limit per test256 megabytes Des ...
- MySQL数据库的高可用性分析
MySQL数据库是目前开源应用最大的关系型数据库,有海量的应用将数据存储在MySQL数据库中.存储数据的安全性和可靠性是生产数据库的关注重点.本文分析了目前采用较多的保障MySQL可用性方案. MyS ...
- Character literal must contain exactly one character -- 一天一点小知识
编程语言高度抽象化以后,错误也越来越让人难以理解了, NET编程最常见的一个错误, Object not set to the reference ,过了好久,才明白过来, 就是不明白为啥微软不说 ...
- BZOJ4446: [Scoi2015]小凸玩密室
用ui,j表示走完i的子树后走到i的深度为j的祖先的兄弟的最小代价: 用vi,j表示走完i的子树后走到i的深度为j的祖先的最小代价,用u算出v. 枚举起点,计算答案. #include<bits ...
- mysql 根据查询结果集更新
声明: MySQL4.0之后的版本可以支持下面sql语句进行更新操作 应用场景: 一个表中的字段需要根据查询结果集进行更新,或者从另一表查询获得 其本质还是更新的数据需要查询获得. 例如: use ...
- layer图层常见属性
把layer常见图层属性总结了一下^-^欢迎大家讨论~~~~来吧 ,代码属性 #import "CZViewController.h" @interface CZViewContr ...
- linux配置IP的方法
Linux系统下如何设置IP地址?我们可以通过命令设定IP的方法,不过此方法的前提条件是用户需root权限.在Linux系统的 /etc/sysconfig/network-script/ifcfg- ...
- Runtime类
Runtime类表示运行时的操作类,是一个封装了JVM进程的类,每一个JVM都对应着一个Runtime类的实例,此实例由JVM运行时为其实例化. //========================= ...