#POJ3897#Maze Stretching(A* + 二分)

9 篇文章 0 订阅
8 篇文章 0 订阅

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.

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.

题意:

给出一个图,正常情况下每次移动的步数都是1,现在你可以将这张图上下比例放缩,水平方向不变(即向上走1步,距离变成k),然后求S到E最短路不大于L的最大放缩。


二分放缩值,然后A*check距离,H值设为曼哈顿距离。


Code:

Status Accepted
Time 32ms
Memory 892kB
Length 2387
Lang G++
Submitted
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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值