胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357 Accepted Submission(s): 2552
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
. 代表路 * 代表墙 @ 代表Ignatius的起始位置 ^ 代表地牢的出口 A-J 代表带锁的门,对应的钥匙分别为a-j a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
@A.B.
a*.*.
*..*^
c..b*
4 5 16
@A.B.
a*.*.
*..*^
c..b*
-1
题解:状态压缩bfs,比赛的时候无限me。。。。心中一万头草泥马奔腾。。。最后大家都说用状态压缩记录钥匙,,,瞬间感觉自己智商负无穷了。。。
其实也不难想,关键以前没怎么用状态压缩;当时想到了用vis存位置以及钥匙数,但是不知道怎么记录,为什么想不到状态压缩呐。。。要长记性了;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
struct Node{
int nx,ny,step;
//int key[11];
int key;
};
char mp[][];
int vis[][][];
int disx[] = {,,,-};
int disy[] = {,-,,};
int n,m,t;
void bfs(int sx,int sy){
memset(vis,,sizeof(vis));
queue<Node>q;
Node a,b;
a.nx = sx;a.ny = sy;
a.step = ;
//for(int i = 0;i < 11;i++)a.key[i] = 0;
a.key = ;
vis[sx][sy][] = ;
q.push(a);
while(!q.empty()){
a = q.front();
q.pop();
for(int i = ; i < ;i++){
b.nx = a.nx + disx[i];
b.ny = a.ny + disy[i];
b.step = a.step + ;
int x = b.nx, y = b.ny, step = b.step;
//for(int i = 0;i < 11;i++)b.key[i] = a.key[i];
b.key = a.key;
if(x < || y < || x >= n || y >= m)continue;
if(mp[x][y] == '*')continue;
if(step >= t)continue;
if(mp[x][y] == '^'){
printf("%d\n",step);
return;
}
// if(vis[x][y] > 10)continue;
if(mp[x][y] >= 'a' && mp[x][y] <= 'j'){
//b.key[mp[x][y] - 'a'] = 1;
int p = << (mp[x][y] - 'a');
b.key |= p;
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
continue;
}
if(mp[x][y] >= 'A' && mp[x][y] <= 'J'){
//if(b.key[mp[x][y] - 'A'] == 0)continue;
int p = << (mp[x][y] - 'A');
if(b.key & p){
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
}
continue;
}
if(!vis[x][y][b.key]){
vis[x][y][b.key] = ;
q.push(b);
}
}
}
puts("-1");
return;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&t)){
int sx,sy;
for(int i = ;i < n; i++){
scanf("%s",mp[i]);
for(int j = ;j < m; j++){
if(mp[i][j] == '@'){
sx = i;
sy = j;
}
}
}
mp[sx][sy] = '.';
bfs(sx,sy);
}
return ;
}
胜利大逃亡(续)(状态压缩bfs)的更多相关文章
- 胜利大逃亡(续)hdu1429(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(三维BFS)
题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...
- 胜利大逃亡(续)(bfs+状态压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...
- 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的地牢里,并在地牢 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- UVA_埃及分数(Hard Version) UVA 12588
Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egy ...
- Implement Trie (Prefix Tree) 解答
Question Implement a trie with insert, search, and startsWith methods. Note:You may assume that all ...
- BFS visit tree
There are two ways to conduct BFS on tree. Solution 1 -- Given level Use recursion to find given lev ...
- 【转】Android LCD(四):LCD驱动调试篇
关键词:android LCD TFTSN75LVDS83B TTL-LVDS LCD电压背光电压 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台 ...
- 监控父元素里面子元素内容变化 DOMSubtreeModified
1监控ul的li的变化情况,并实时输出li的长度 布局: <ul id="isSelected"></ul> <span id="modal ...
- PreferenceFragment 使用 小结
Perference也就是我们常说的偏好设置,首选项设置,能够自己主动保存一些数据,比如我们在上一次使用的时候的一些内容,则在下一次启动后依旧生效,而不须要再进行配置.当用户改变设置时,系统就会更新S ...
- 【高精度递推】【HDU1297】Children’s Queue
Children's Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- httpwatch 9.3怎么在ie 8上看不到
首先,确认HttpWatch Basic加载项是否启动:打开IE,单击工具图标并选择管理加载项.确认HttpWatch Basic的状态是已启用,点击关闭.之后打开某个网页,在页面空白处右击选择Htt ...
- android中跨进程通讯的4种方式
转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...
- url 的httppost 和http get ,put,delect
URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源 ,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作 .到这里 ...