hdoj 1429 胜利大逃亡(续) 【BFS + 状态压缩】
2015-08-13 18:09
405 查看
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6645 Accepted Submission(s): 2312
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
思路:最多出现11个钥匙,用二进制来表示可能获得的所有情况。设置三维数组vis[ x ][ y ][ key ] 存储到达位置(x,y)时拥有钥匙个数的状态,用这个数组进行BFS过程中的状态标记。
代码一:BFS + 优先队列 (后面代码是没用优先队列的) 951ms。。。
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 21 using namespace std; struct Node { int x, y, step, key; friend bool operator < (Node a, Node b) { return a.step > b.step; } }; bool vis[MAXN][MAXN][1<<11];//最多11个钥匙 char Map[MAXN][MAXN]; int N, M, T; int ans; int sx, sy;//起点 终点 bool judge(Node a) { return a.x >= 0 && a.x < N && a.y >= 0 && a.y < M && Map[a.x][a.y] != '*'; } bool BFS(int x, int y) { priority_queue<Node> Q; memset(vis, false, sizeof(vis)); int move[4][2] = {0,1, 0,-1, 1,0, -1,0}; Node now, next; now.x = x, now.y = y, now.step = 0, now.key = 0; Q.push(now); vis[now.x][now.y][now.key] = true; while(!Q.empty()) { now = Q.top(); Q.pop(); if(Map[now.x][now.y] == '^') { ans = now.step; return true; } if(now.step > T)//路已经死了 continue; for(int k = 0; k < 4; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; next.step = now.step + 1; if(judge(next))//可以走 { if(Map[next.x][next.y] >= 'a' && Map[next.x][next.y] <= 'j')//钥匙 { next.key = now.key | (1 << (Map[next.x][next.y] - 'a'));//得到当前状态 if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } else if(Map[next.x][next.y] >= 'A' && Map[next.x][next.y] <= 'J') { next.key = now.key;//得到当前状态 if(next.key & (1 << (Map[next.x][next.y] - 'A')))//有该门的钥匙 { if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } } else//路 { next.key = now.key;//得到当前状态 if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } } } } return false; } int main() { while(scanf("%d%d%d", &N, &M, &T) != EOF) { for(int i = 0; i < N; i++) { scanf("%s", Map[i]); for(int j = 0; j < M; j++) { if(Map[i][j] == '@') sx = i, sy = j; } } if(BFS(sx, sy) && ans < T) printf("%d\n", ans); else printf("-1\n"); } return 0; }
代码二:没有优先队列 跑了256ms
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 21 using namespace std; struct Node { int x, y, step, key; }; bool vis[MAXN][MAXN][1<<11];//最多11个钥匙 char Map[MAXN][MAXN]; int N, M, T; int ans; int sx, sy;//起点 终点 bool judge(Node a) { return a.x >= 0 && a.x < N && a.y >= 0 && a.y < M && Map[a.x][a.y] != '*'; } bool BFS(int x, int y) { queue<Node> Q; memset(vis, false, sizeof(vis)); int move[4][2] = {0,1, 0,-1, 1,0, -1,0}; Node now, next; now.x = x, now.y = y, now.step = 0, now.key = 0; Q.push(now); vis[now.x][now.y][now.key] = true; while(!Q.empty()) { now = Q.front(); Q.pop(); if(Map[now.x][now.y] == '^') { ans = now.step; return true; } if(now.step > T)//路已经死了 continue; for(int k = 0; k < 4; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; next.step = now.step + 1; if(judge(next))//可以走 { if(Map[next.x][next.y] >= 'a' && Map[next.x][next.y] <= 'j')//钥匙 { next.key = now.key | (1 << (Map[next.x][next.y] - 'a'));//得到当前状态 if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } else if(Map[next.x][next.y] >= 'A' && Map[next.x][next.y] <= 'J') { next.key = now.key;//得到当前状态 if(next.key & (1 << (Map[next.x][next.y] - 'A')))//有该门的钥匙 { if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } } else//路 { next.key = now.key;//得到当前状态 if(!vis[next.x][next.y][next.key])//该状态没有出现过 { vis[next.x][next.y][next.key] = true; Q.push(next); } } } } } return false; } int main() { while(scanf("%d%d%d", &N, &M, &T) != EOF) { for(int i = 0; i < N; i++) { scanf("%s", Map[i]); for(int j = 0; j < M; j++) { if(Map[i][j] == '@') sx = i, sy = j; } } if(BFS(sx, sy) && ans < T) printf("%d\n", ans); else printf("-1\n"); } return 0; }
相关文章推荐
- spring mvc 的搭建
- 【Android和iOS】快速切换到主线程更新UI
- lucene对文档的加权
- unity, 对于Debug.Log输出的log,可以双击定位到代码
- UITabBarController
- redhat7.1 vsftp root用户上传
- 如何通过防火墙策略限制对外扫描行为
- rabbitmq——prefetch count
- 【读书笔记】iOS-忽略编译警告
- 30-CSS-09-CSS(CSS的布局-漂浮)
- 【读书笔记】iOS-忽略编译警告
- 记录一些简单的matlab用法
- AngularJS(三)——在多个AngularJS controller之间共享数据
- 设计模式之四大原则
- 30-CSS-08-CSS(CSS的盒子模型)
- FSL BC3770 快速使用指南
- Unix编程艺术札记版本
- 全天各个时间段产品销量情况统计
- nyoj 92 图像有用区域
- eclipse构建maven的web项目