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的地牢里,并在地牢的某些地方安装了带 ...
随机推荐
- java里面时间差比较
currentTime.before(this.getLastActivateTime(time))//返回true 或者false public Date getLastActivateTime(t ...
- POJ1426 Find The Multiple (宽搜思想)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24768 Accepted: 102 ...
- UVA10054The Necklace (打印欧拉路)
题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...
- ubuntu --- shortcut key
Linux系统下图形界面与Linux命令行模式的切换的方法 由图形转换到控制台模式:ctrl+alt+f1~f6(同时按下3秒钟不要马上松开)....由控制台转向图形模式是:alt+f7 快捷键(ub ...
- CentOS设置默认启动命令行(不启动图形界面)
Linux 启动的时候可以选择纯文本或者是窗口环境,这就牵涉了运行等级这个问题.Linux 默认提供了 7 个 Run level 给我们使用,其中我们最常用的就是 run level3 和run l ...
- Install latest R for ubuntu
### delete old version rm -rf /usr/local/lib/R /usr/lib/R ~/**/R sudo apt-get autoremove rstudio sud ...
- BZOJ4620: [Wf2016]What Really Happened on Mars?
题意比较难懂?反正我为此特地查了优先级倒置和优先级置顶协议是什么. 读懂题以后就好办了,直接模拟即可. 由于数据范围较小,写得比较暴力,应该还有很大优化空间. #include<cstdio&g ...
- SQL 字段保留下划线后部分
select SUBSTRING(b.SUMMARY,0,charindex('_',b.SUMMARY))as SUMMARY from UltimusDB.dbo.INCIDENTS b
- WinForm------PopupMenu控件的使用
转载: http://www.cnblogs.com/xlx0210/archive/2010/07/14/1777366.html
- ubuntu下vim输入中文和中文显示
安装和配置VIM,参考 http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...