EOJ:Maze Stretching
Maze Stretching
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submits: 107 | Accepted: 20 |
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).
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).
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).
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
2
2.5 4
#####
#S #
# E#
#####
21 13
############
#S## #E#
# ## # # #
# # # # #
### # # # #
# # # # #
# ## # # #
## # # # #
### # # # #
## # # # #
# ## # #
# # #
############
Sample Output
Case #1: 50.000%
Case #2: 21.053%
Hint
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.
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.
____________________________________________________________________________________________________
题解:
题目要求的是改变之后的最短路刚好等于L,但是题目的HINT让人觉得是先求最短路,然后改变百分比使得其长度变成L。
题目理解对了就没什么问题了。
主要是二分答案的思想。
DIJ求最短路套了交大模板。
代码
1 #include<stdio.h>
2 #include<math.h>
3 #include<string.h>
4 const int inf=1000000000;
5 #define maxm 100000
6 #define maxn 100005
7 int n,m,len,next[maxm],ev[maxm],ew[maxm];
8 int mk[maxn],nbs[maxn],ps[maxn],heap[maxn];
9 char str[105][105];
10 int r,c,i,j,t,tt,s,e,num;
11 double ans,ll,rr,mm,value[maxn],l;
12 void update(int r)
13 {
14 int q=ps[r],p=q>>1;
15 while (p&&value[heap[p]]>value[r])
16 {
17 ps[heap[p]]=q;
18 heap[q]=heap[p];
19 q=p;
20 p=q>>1;
21 }
22 heap[q]=r;ps[r]=q;
23 }
24 int getmin()
25 {
26 int ret=heap[1],p=1,q=2,r=heap[len--];
27 while(q<=len)
28 {
29 if (q<len&&value[heap[q+1]]<value[heap[q]]) q++;
30 if (value[heap[q]]<value[r])
31 {
32 ps[heap[q]]=p;
33 heap[p]=heap[q];
34 p=q;q=p<<1;
35 }
36 else break;
37 }
38 heap[p]=r;
39 ps[r]=p;
40 return ret;
41 }
42 void dijkstra(int src,int dst,double l)
43 {
44 int i,j,u,v;
45 double lv;
46 for (i=1;i<=n;i++)
47 {
48 value[i]=inf;
49 mk[i]=ps[i]=0;
50 }
51 value[src]=0;
52 heap[len=1]=src;
53 ps[src]=1;
54 while (!mk[dst])
55 {
56 if (len==0) return;
57 u=getmin();
58 mk[u]=1;
59 for (j=nbs[u];j;j=next[j])
60 {
61 v=ev[j];
62 if (ew[j]==-1)
63 lv=l;
64 else lv=ew[j];
65 if (!mk[v]&&value[u]+lv<value[v])
66 {
67 if (ps[v]==0)
68 {
69 heap[++len]=v;
70 ps[v]=len;
71 }
72 value[v]=value[u]+lv;
73 update(v);
74 }
75 }
76 }
77 }
78 void creat(int u,int v,int w)
79 {
80 next[++num]=nbs[u];
81 nbs[u]=num;
82 ev[num]=v;
83 ew[num]=w;
84 }
85 int main()
86 {
87 scanf("%d",&tt);
88 for (t=1;t<=tt;t++)
89 {
90 memset(nbs,0,sizeof(nbs));
91 memset(next,0,sizeof(next));
92 scanf("%lf%d",&l,&r);
93 getchar();
94 for (i=0;i<r;i++)
95 gets(str[i]);
96 c=strlen(str[0]);
97 num=0;
98 for (i=0;i<r-1;i++)
99 for (j=0;j<c-1;j++)
100 {
101 if (str[i][j]=='S')
102 s=j+i*c+1;
103 if (str[i][j]=='E')
104 e=j+i*c+1;
105 if (str[i][j]!='#')
106 {
107 if (str[i+1][j]!='#')
108 {
109 creat(j+c*i+1,j+c*(i+1)+1,-1);
110 creat(j+c*(i+1)+1,j+c*i+1,-1);
111 }
112 if (str[i][j+1]!='#')
113 {
114 creat(j+c*i+1,j+2+c*i,1);
115 creat(j+2+c*i,j+c*i+1,1);
116 }
117 }
118 }
119 rr=1000.000;
120 ll=0.000;
121 n=r*c;
122 while (1)
123 {
124 mm=(ll+rr)/2;
125 dijkstra(s,e,mm/100);
126 if (fabs(value[e]-l)<0.000001)
127 {
128 ans=mm;
129 break;
130 }
131 if (value[e]>l) rr=mm;
132 else ll=mm;
133 }
134 printf("Case #%d: %.3lf%%\n",t,ans);
135 }
136 return 0;
137 }