poj3897Maze Stretching(二分+记忆化搜索)
来源:互联网 发布:中国游戏中心mac版 编辑:程序博客网 时间:2024/04/28 23:31
Maze Stretching
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 708 Accepted: 187
Description
Usually the path in a maze is calculated as the sum of steps taken from the starting point until the ending point, assuming that the distance of one step is exactly 1. Lets assume that we could “stretch” (shorten or extend) the maze in vertical dimension (north-south). By stretching, we are just changing the passed distance between two cells. (it becomes X instead of one). We have a two dimensional maze which has '#' for walls, 'S' in the starting cell and 'E' at the ending cell.
Due to outside conditions, we need to make the shortest path to be exactly L in size. We are not allowed to change the maze configuration, nor to make any changes in the horizontal dimension. We are only allowed to stretch the vertical dimension, and that can be done by any percentage.
Find the percentage of the stretch P, for which the shortest path of the maze will be exactly L.
Due to outside conditions, we need to make the shortest path to be exactly L in size. We are not allowed to change the maze configuration, nor to make any changes in the horizontal dimension. We are only allowed to stretch the vertical dimension, and that can be done by any percentage.
Find the percentage of the stretch P, for which the shortest path of the maze will be exactly L.
Input
First line of the input contains the number of test cases. For each test case, firstly two numbers L and N are given, where L is the required length of the shortest path and N is the number of lines that are given describing the maze. The following N lines describes the maze such as that each line describes a row of the maze. (Each row length is the horizontal dimension of the maze).
Output
For each test case output the percentage of the stretch in the following format:
Case #K: P%
- P should have leading zero if the number is between 0 and 1.
- P should be rounded up on 3 decimals, and always formatted on 3 decimals (with trailing zeros if needed).
Case #K: P%
- P should have leading zero if the number is between 0 and 1.
- P should be rounded up on 3 decimals, and always formatted on 3 decimals (with trailing zeros if needed).
Sample Input
22.5 4######S ## E######21 13#############S## #E## ## # # ## # # # #### # # # ## # # # ## ## # # ### # # # #### # # # ### # # # ## ## # ## # #############
Sample Output
Case #1: 50.000%Case #2: 21.053%
Hint
Constraints
The height and width of the maze are maximum 100 cells.
All the lines describing one maze are the same size.
There will always be a solution.
The result will be between 0.000 and 1000.000 inclusive
There will be no direct horizontal only path connecting 'S' and 'E'. (the result is always unique).
Explanation of the first test case in the example: On the original maze, the length of the shortest path is 3 because there are two horizontal steps and a vertical one. Our goal is to make the length of the shortest path to be 2.5. That’s why we have to “stretch” the vertical dimension of the maze by a percentage value less than 100. In this case it is 50% which actually changes the vertical distance between two cells to 0.5.
The height and width of the maze are maximum 100 cells.
All the lines describing one maze are the same size.
There will always be a solution.
The result will be between 0.000 and 1000.000 inclusive
There will be no direct horizontal only path connecting 'S' and 'E'. (the result is always unique).
Explanation of the first test case in the example: On the original maze, the length of the shortest path is 3 because there are two horizontal steps and a vertical one. Our goal is to make the length of the shortest path to be 2.5. That’s why we have to “stretch” the vertical dimension of the maze by a percentage value less than 100. In this case it is 50% which actually changes the vertical distance between two cells to 0.5.
Source
Southeastern European Regional Programming Contest 2009
题目大意:给一个纵向能伸缩的迷宫,给一个长度,求起点到终点最短距离恰好为给定长度的地图的纵向伸缩比例。
题目分析:由于地图可以伸缩,那么搜索的时候最先到达终点所走的路程就不一定是最短的,因为纵向单位长度不是1了。所以不能直接搜。根据题目给定的范围,可以确定地图伸缩的比例范围,然后可以二分地图的比例,然后搜索,搜索的时候可以用优先队列直接往终点搜,如果用普通bfs,要记忆化搜索,如果到达某点距离比之前到达的距离小,则更新之,最后找到起点到终点的最短路径。
注意输出浮点数在poj上用c++提交。
详情请见代码:
#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int N = 105;const int M = 1000005;const double eps = 1e-8;double l,p,ans;int n,m;int si,sj,ei,ej;char g[N][N];double step[N][N];bool flag[N][N];struct node{ int x,y;}ss,now;int dx[] = {1,-1,0,0};int dy[] = {0,0,1,-1};queue<node>q;double bfs(){ while(!q.empty()) q.pop(); int i,tx,ty; double add; ss.x = si; ss.y = sj; memset(flag,0,sizeof(flag)); for(tx = 0;tx < n;tx ++) for(ty = 0;ty < m;ty ++) step[tx][ty] = 1000000; flag[si][sj] = 1; step[si][sj] = 0; q.push(ss); while(!q.empty()) { now = q.front(); flag[now.x][now.y] = 0;//同一个节点可以访问多次 q.pop(); for(i = 0;i < 4;i ++) { add = i < 2?p:1; int tx = now.x + dx[i]; int ty = now.y + dy[i]; if(tx < 0 || ty < 0 || tx >= n || ty >= m || g[tx][ty] == '#' || flag[tx][ty] || step[tx][ty] < step[now.x][now.y] + add) continue; flag[tx][ty] = 1; ss.x = tx; ss.y = ty; step[tx][ty] = step[now.x][now.y] + add; q.push(ss); } } return step[ei][ej];}int main(){ int i,j,t; int cas = 0; scanf("%d",&t); while(t --) { scanf("%lf%d",&l,&n); getchar(); for(i = 0;i < n;i ++) gets(g[i]); m = strlen(g[0]); for(i = 0;i < n;i ++) { for(j = 0;j < m;j++) { if(g[i][j] == 'S') { si = i; sj = j; } if(g[i][j] == 'E') { ei = i; ej = j; } } } double ll,rr,tt; ll = 0; rr = 1000; while(rr - ll > eps) { p = (ll + rr)/2; tt = bfs(); if(tt - l > eps) rr = p; else ll = p; } printf("Case #%d: ",++ cas); printf("%.3lf%%\n",p * 100); } return 0;}//304K125MS
- poj3897Maze Stretching(二分+记忆化搜索)
- 【A*搜索,BFS搜索】poj3897Maze Stretching
- poj 3897 Maze Stretching 二分+A*搜索
- 【图论】【启发式搜索】【二分查找】[POJ 3897]Maze Stretching
- 【搜索】[POJ3897]Maze Stretching
- hdu-3943[数位dp(记忆化搜索)+二分]
- poj3897 Maze Stretching 二分+bfs
- [POJ3897] Maze Stretching (二分)(A*)
- POJ3897 Maze Stretching (二分+BFS)
- Maze Stretching Poj3897 二分+BFS
- #POJ3897#Maze Stretching(A* + 二分)
- 记忆化搜索例题
- PKU1088---记忆化搜索
- PKU1088 记忆化搜索
- HDU1078 记忆化搜索
- HDU1501 记忆化搜索
- HDU1978 记忆化搜索
- poj1661 记忆化搜索
- 从郭美美霸气侧漏看项目管理之项目经理防身术
- poj 3897 Maze Stretching (二分+bfs+二维判重)
- iOS7人机界面指南 – ISUX原创翻译
- flex 访问pdf.swf 控制上一页,下一页demo
- 【2013Esri全球用户大会精彩案例】Dartmouth Atlas卫生保健项目-GIS特别贡献奖
- poj3897Maze Stretching(二分+记忆化搜索)
- A successful Git branching model
- HDOJ 1421 搬寝室 杭电 ACM
- 大数据应用之Windows平台Hbase客户端Eclipse环境搭建-Java版
- 写了一个内核模块 实现简单的类似ps命令
- 5个数求最值
- [转载]堆和栈得区别--向WXP学习
- Hidden Features of Xcode
- 【模板】二分