poj 2251 Dungeon Master(多起点bfs)
来源:互联网 发布:阿里大数据查询 编辑:程序博客网 时间:2024/06/16 17:38
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Escaped in 11 minute(s).Trapped!
思路:1 先寻找‘#’连通块的个数
2 如果大于3块, -1 ,因为最多放两堆火。
3 如果两块,每人放火烧一块,遍历所有“#”,两块各自取最小值,两最小值取较大作为结果。
4 如果一块,就是双起点广搜了,排列组合选择两块同时开始,具体做法就是同时压入队列,所有 时间取最小值。
#include <cstdio>#include <queue>#include <map>#include <stack>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int mk[15][15];char mp[15][15];int m,n,T;typedef struct node{ int x,y; int t; node(int x_=0,int y_=0,int t_=0) { x=x_,y=y_,t=t_; }}node;queue <node> q;int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};void bfs(int x,int y,int mark){ node tmp(x,y,0); q.push(tmp); mk[x][y] = mark; T = -1; while(!q.empty()) { tmp = q.front(); T = tmp.t; q.pop(); for(int i = 0; i < 4; i++) { int x_ = tmp.x+dir[i][0]; int y_ = tmp.y+dir[i][1]; if(x_ < 1 || y_ < 1|| x_ > m || y_ > n) continue; if(mp[x_][y_] == '.') continue; if(mk[x_][y_] == mark) continue; mk[x_][y_] = mark; node nd(x_,y_,tmp.t+1); q.push(nd); } }}int main(){ int C,index = 1; cin >> C; while(C--) { int mark = 0;//标记连通块个数 cin >> m >> n; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) cin >> mp[i][j]; memset(mk,0,sizeof(mk)); for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if(mp[i][j] == '#' && !mk[i][j]) { while(!q.empty())q.pop(); bfs(i,j,++mark); } if(mark >= 3) { printf("Case %d: -1\n",index++); continue; } if(mark == 2) { int k1 = 1,k2 = 2;//作用是保证下次还能找到同一连通块的标志。 int a[2] ={110,110}; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if(mp[i][j] == '#' && mk[i][j] == k1) { k1+=2; while(!q.empty())q.pop(); bfs(i,j,k1); a[0] = min(a[0],T); } else if(mp[i][j] == '#'&& mk[i][j] == k2) { k2+=2; while(!q.empty())q.pop(); bfs(i,j,k2); a[1] = min(a[1],T); } printf("Case %d: %d\n",index++,max(a[0],a[1])); } else { int k = 1,l = 0,time = 110; node a[110]; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if(mp[i][j] == '#' && mk[i][j] == k) { a[l].x =i,a[l++].y = j; } for(int i = 0; i < l; i++) for(int j = i; j < l; j++) { while(!q.empty())q.pop(); if(j!=i) q.push(a[j]); bfs(a[i].x,a[i].y,++k); time = min(time,T); } printf("Case %d: %d\n",index++,time); } } return 0;}
阅读全文
1 0
- poj 2251 Dungeon Master(多起点bfs)
- POJ 2251 Dungeon Master (三维BFS)
- poj-2251-Dungeon Master(bfs)
- poj 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master(三维BFS)
- POJ 2251-Dungeon Master(BFS)
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master(bfs)
- POJ - 2251 - Dungeon Master (简单BFS)
- POJ 2251 Dungeon Master (BFS)
- poj 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master(BFS)
- 【POJ】-2251-Dungeon Master(BFS+队列)
- POJ 2251 - Dungeon Master(BFS)
- poj 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master(三维BFS)
- JS学习第四天
- java操作Redis数据库的redis工具,RedisUtil,jedis工具JedisUtil,JedisPoolUtil
- 悬线法求最大子矩阵(洛谷P1169 [ZJOI2007]棋盘制作 bzoj1057)
- dfs(洛谷1019 单词接龙NOIp2000提高组第三题)
- lca(洛谷P3379 最近公共祖先(LCA))
- poj 2251 Dungeon Master(多起点bfs)
- LCA 最近公共祖先
- 树形dp(人品问题NOIP17提高模拟训练3)
- 使用SSH框架进行开发的时候,由于延迟加载引起的no Session错误解决方法
- 立体井字棋NOIP17提高模拟训练5
- 所有的M数NOIP17提高模拟训练5
- 前端初学学习进程II
- 树形dp(数字转换NOIP17提高模拟训练4)
- 前缀和(vijos1090连续数之和)