Description
Usually the path in a maze was calculated as the sum of steps taken from the starting point until the ending point, assumin G. 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 is just changing the passed distance between the cells. (It becomes X instead of one). We have a dimensional maze which with ' # ' for Walls, ' S ' on the starting cell and ' E ' at the ending cell.
Due to outside conditions, we need to make the shortest path to is exactly L in size. We is not a allowed to a change of the maze configuration, nor to do any changes in the horizontal dimension. We are a allowed to stretch the vertical dimension, and that can is done by any percentage.
Find the percentage of the stretch P, for which the shortest path of the maze would be exactly L.
Input
First line of the input contains the number of test cases. For each test case, firstly-numbers L and N are given, where L was the required length of the shortest path and n is th E number of lines that is 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 all test case output The percentage of the stretch in the following format:
Case #K: p%
-P should has leading zero if the number is between 0 and 1.
-P should is rounded up on 3 decimals, and all 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 cells.
All the lines describing one maze is the same size.
There'll always be a solution.
The result would be between 0.000 and 1000.000 inclusive
There'll be is no direct horizontal only path connecting ' 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 was 3 because there is both horizontal steps and a vertical one. Our goal are to do the length of the shortest path to be 2.5. That's why we had 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 both cells to 0.5. Test Instructions: gives a graph that marks the start and end points, and a telescopic transformation of the longitudinal axis so that the shortest path from the starting point to the end point equals the given value. Analysis: The possibility of two-dimensional scaling, for each type of scaling has a corresponding to the shortest, then the total distance of Val by the line mark, record the minimum distance of the BFS to reach each point, and finally return to the shortest distance to the end, continue to two points. At first, it is simple to think that the shortest circuit must be fixed, but not, see a picture ######## S ## # # ## #E ## # # # # ######### #此时当纵向伸缩足够小时, it becomes the shortest path from the left. then continue to simply think that using the priority queue to record step will solve the problem ... Actually, look at the picture ... ### ###### ### e### ###### ### This can happen when approaching E from four directions: horizontal and vertical at the same distance from the end of the line while vertical or horizontal to the end of the step is smaller, of course, there may be other diagrams have various options to reach E and step smaller (brain tonic). * so if you want to do this, just go through the whole map and record the shortest path value.。 q: Why do you think of two points? A: Feel ... It's pretty interesting to think about it
1#include <cstring>2#include <cstdio>3#include <iostream>4#include <algorithm>5#include <queue>6 7 #defineEPS 1e-98 #defineINF 0x3f3f3f3f9 using namespacestd;Ten One structnode A { - intx, y; - DoubleStep; theFriendBOOL operator<(node A,node B) - { - returnA.step>B.step; - } + }; - +Priority_queue<node>que; A at Charg[ the][ the]; - intvis[ the][ the]; - Doubleval[ the][ the];//Store the shortest path value for each BFS - intdir[][5]={{-1,0},{1,0},{0,-1},{0,1}}; - intX,y,xx,yy; - intn,m; in Doubleaim; - to + BOOLJudgeintAintb) - { the if(a>=0&&a<n&&b>=0&&B<M)return true; * return false; $ }Panax Notoginseng - DoubleBFsDoublemid) the { + while(Que.size ()) Que.pop (); Amemset (Vis,0,sizeofvis); the + for(intI=0; i<n;i++) - for(intj=0; j<m;j++) $val[i][j]=INF; $ - node K; -k.x=x; thek.y=y; -k.step=0;Wuyi Que.push (k); the - while(Que.size ()) Wu { -k=que.top (); About Que.pop (); $ - for(intI=0;i<4; i++) - { -Node temp=K; Atemp.x+=dir[i][0]; +temp.y+=dir[i][1]; the if(Judge (TEMP.X,TEMP.Y) &&g[temp.x][temp.y]!='#') - { $vis[temp.x][temp.y]=1; the Doublet_t; the if(i==0|| i==1) t_t=mid; the Elset_t=1; thetemp.step+=t_t;//= =, - in if(!vis[temp.x][temp.y]| | (vis[temp.x][temp.y]&&temp.step<VAL[TEMP.X][TEMP.Y])) not accessed or accessed but at this point the value is small and logged. the { thevis[temp.x][temp.y]=1; Aboutval[temp.x][temp.y]=Temp.step; the Que.push (temp); the } the + } - } the }Bayi returnVal[xx][yy]; the } the - intMain () - { the intT; the Doubleans; thescanf"%d",&T); the for(intk=1; k<=t;k++) - { thescanf"%lf%d",&aim,&n); the GetChar (); the for(intI=0; i<n;i++)94 gets (G[i]); the theM=strlen (g[0]); the 98 for(intI=0; i<n;i++) About for(intj=0; j<m;j++) - if(g[i][j]=='S')101 {102x=i;103y=J;104 } the Else if(g[i][j]=='E')106 {107xx=i;108yy=J;109 } the 111 the DoubleR=0, l=1000000;113 while(r+eps<l) the { the DoubleMid= (r+l)/2; the if(BFS (Mid/ -) >aim) l=mid;117 ElseR=mid;118 }119ans=R; -printf"Case #%d:%.3lf%%\n", K,ans);121 }122}
Maze stretching Poj3897 two points +bfs