Maze Stretching
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1567 | Accepted: 382 |
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
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.
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.
题意:
给出一个图,正常情况下每次移动的步数都是1,现在你可以将这张图上下比例放缩,水平方向不变(即向上走1步,距离变成k),然后求S到E最短路不大于L的最大放缩。
二分放缩值,然后A*check距离,H值设为曼哈顿距离。
Code:
Status | Accepted |
---|---|
Time | 32ms |
Memory | 892kB |
Length | 2387 |
Lang | G++ |
Submitted | 2017-10-06 15:37:40 |
Shared |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
const int Max = 100;
const double eps = 1e-6;
const double INF = 1e10;
struct node{
int x, y;
double f, g;
node(){}
node(int a, int b, double c, double d){x = a, y = b, f = c, g = d;}
bool operator > (const node X)const{
return f > X.f;
}
}S, T;
int N, M;
double L;
int dd[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char map[Max + 5][Max + 5];
double dis[Max + 5][Max + 5];
double H(int x, int y, double k){
double rt = abs(T.x - x) * k + abs(T.y - y);
return rt;
}
bool Inarea(int x, int y){
if(x < 1 || y < 1 || x > N || y > M) return 0;
return map[x][y] != '#';
}
priority_queue<node, vector<node>, greater<node> >Q;
bool Astar(double mid){
while(! Q.empty()) Q.pop();
for(int i = 1; i <= N; ++ i)
for(int j = 1; j <= M; ++ j) dis[i][j] = INF;
dis[S.x][S.y] = 1;
Q.push(S);
while(! Q.empty()){
node tmp = Q.top(); Q.pop();
for(int i = 0; i < 4; ++ i){
int xx = tmp.x + dd[i][0];
int yy = tmp.y + dd[i][1];
if(Inarea(xx, yy)){
node t = node(xx, yy, tmp.f, tmp.g);
if(i < 2) t.g += mid;
else t.g += 1;
t.f = t.g + H(xx, yy, mid);
if(t.g < dis[xx][yy]) dis[xx][yy] = t.g;
else continue;
if(t.x == T.x && t.y == T.y){
if(t.g <= L) return 1;
return 0;
}
Q.push(t);
}
}
}
return 0;
}
int main(){
int Ti;
scanf("%d", &Ti);
for(int cas = 1; cas <= Ti; ++ cas){
scanf("%lf%d", &L, &N);getchar();
for(int i = 1; i <= N; ++ i)
gets(map[i] + 1);
M = strlen(map[1] + 1);
for(int i = 1; i <= N; ++ i)
for(int j = 1; j <= M; ++ j){
if(map[i][j] == 'S') S = node(i, j, 0, 0);
else if(map[i][j] == 'E') T = node(i, j, 0, 0);
}
double l = 0.0, r = 11.0, mid;
while(r - l > eps){
mid = (l + r) / 2.0;
if(Astar(mid)) l = mid;
else r = mid;
}
printf("Case #%d: %.3f%%\n", cas, mid * 100);
}
return 0;
}