胜利大逃亡(续)

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

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

hdu.1429.胜利大逃亡(续)(bfs + 0101011110)的更多相关文章

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

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...

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

    又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...

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

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

  5. hdu 1429 胜利大逃亡(续)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...

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

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

  7. 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 ...

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

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

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

    Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带 ...

随机推荐

  1. C++ 之 const references

    extraction from The C++ Programming Language 4th. ed., Section 7.7 References, Bjarne Stroustrup To ...

  2. 数据结构2 静态区间第K大/第K小

    给定数组$A[1...N]$, 区间$[L,R]$中第$K$大/小的数的指将$A[L...R]$中的数从大到小/从小到大排序后的第$K$个. "静态"指的是不带修改. 这个问题有多 ...

  3. Response 对象

    Response 对象用于将数据从服务器发送回浏览器. 页面跳转并传递参数 Response.Redirect("~/welcome.aspx?parameter1=one&para ...

  4. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  5. 一个WPF控件 诡异的MouseEvent 。

    背景: private System.Windows.Controls.Border _borderTouch; private bool _mouseDown = false;  private S ...

  6. JS性能方面--内存管理及ECMAScript5 Object的新属性方法

    Delete一个Object的属性会让此对象变慢(多耗费15倍的内存) var o = { x: 'y' }; delete o.x; //此时o会成一个慢对象 o.x; // var o = { x ...

  7. Django_collections01

    python manage.py flush python manage.py changepassword username python manage.py createsuperuser pyt ...

  8. JavaWeb---总结(十)HttpServletRequest对象(一)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  9. EasyUI——常见用法总结

    1. 使用 data-options 来初始化属性. data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写 ...

  10. C#------EntityFramework实体加载数据库SQLServer(MySQL)

    一.SQLServer数据库创建表Company,包含ID,CName,IsEnabled三列 二.(1)VS新建一个DXApplication工程,名为CompanyManageSystem (2) ...