Maze Stretching
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. 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). Sample Input 2 2.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. Source
Southeastern European Regional Programming Contest 2009
|
这是暑假集训队积分赛上出现的一个题目,这次积分赛打的很糟糕,我就不多说了,以后要更加努力!
题意:将迷宫中的y轴拉伸 使得出发点到终点的最小距离为L。
思维误区:先搜索得到最短路 再拉伸 这样是不对的 想一想 为什么
正确思路:先二分拉伸的百分比 然后再搜索最短路 看最短路和L的关系取上届和下界
ps:有了正确思路还要注意一个track 就是用优先队列最先收到的也不一定是答案 还有就是不能简单的二维数组判重
代码;
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define maxn 105 #define eps 1e-10 using namespace std; int n,m; int sx,sy,ex,ey; int xx,yy; double l,ly,dist; bool vis[maxn][maxn][2]; // 三维数组判重 最后一位表示x轴还是y轴 int mp[maxn][maxn]; int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; char s[maxn]; struct Node { int x,y; int stepx; double stepy; friend bool operator <(Node x1,Node x2) { return x1.stepx+x1.stepy>x2.stepx+x2.stepy; } }cur,now; priority_queue<Node>q; void bfs() { int i,j,nx,ny,nstx,tx,ty; double nsty; memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); cur.x=sx; cur.y=sy; cur.stepx=0; cur.stepy=0; dist=1000000000; q.push(cur); while(!q.empty()) { now=q.top(); q.pop(); nx=now.x; ny=now.y; nstx=now.stepx; nsty=now.stepy; if(nstx+nsty>=dist) return ; // 搜到这里才结束 if(nx==ex&&ny==ey) { if(dist>nstx+nsty) dist=nstx+nsty; } for(i=0;i<4;i++) { tx=nx+dx[i]; ty=ny+dy[i]; if(!mp[tx][ty]) { if(i==0||i==1) { if(vis[tx][ty][0]) continue ; } else { if(vis[tx][ty][1]) continue ; } if(i==0||i==1) { vis[tx][ty][0]=1; } else { vis[tx][ty][1]=1; } cur.x=tx; cur.y=ty; if(i==0||i==1) { cur.stepx=nstx; cur.stepy=nsty+1.0*ly; } else { cur.stepx=nstx+1; cur.stepy=nsty; } q.push(cur); } } } } void solve() { int i,j; double le=0,ri=100,mid; while(ri-le>eps) // 二分拉伸状态 { mid=ly=(le+ri)/2.0; bfs(); if(l>dist) le=mid; else ri=mid; // printf("le:%lf ri:%lf\n",le,ri); } le*=100; printf("%.3f%%\n",le); } int main() { int i,j,t,k; scanf("%d",&t); for(k=1;k<=t;k++) { scanf("%lf%d",&l,&n); getchar(); memset(mp,1,sizeof(mp)); for(i=1;i<=n;i++) { gets(s); for(j=1;s[j-1]!='\0';j++) { if(s[j-1]==' ') mp[i][j]=0; else if(s[j-1]=='S') { mp[i][j]=0; sx=i; sy=j; } else if(s[j-1]=='E') { mp[i][j]=0; ex=i; ey=j; } } } m=strlen(s); printf("Case #%d: ",k); solve(); } return 0; }