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

时间:2021-09-17 16:42:12

胜利大逃亡(续)

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

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<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明白了,不过果然变得很麻烦,给过还是。。。。。

附上用二进制保存钥匙状态的程序。