poj3897Maze Stretching(二分+记忆化搜索)

Maze Stretching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 708   Accepted: 187

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

题目大意:给一个纵向能伸缩的迷宫,给一个长度,求起点到终点最短距离恰好为给定长度的地图的纵向伸缩比例。

题目分析:由于地图可以伸缩,那么搜索的时候最先到达终点所走的路程就不一定是最短的,因为纵向单位长度不是1了。所以不能直接搜。根据题目给定的范围,可以确定地图伸缩的比例范围,然后可以二分地图的比例,然后搜索,搜索的时候可以用优先队列直接往终点搜,如果用普通bfs,要记忆化搜索,如果到达某点距离比之前到达的距离小,则更新之,最后找到起点到终点的最短路径。

注意输出浮点数在poj上用c++提交。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 105;
const int M = 1000005;
const double eps = 1e-8;
double l,p,ans;
int n,m;
int si,sj,ei,ej;
char g[N][N];
double step[N][N];
bool flag[N][N];
struct node
{
    int x,y;
}ss,now;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

queue<node>q;

double bfs()
{
    while(!q.empty())
        q.pop();
    int i,tx,ty;
    double add;
    ss.x = si;
    ss.y = sj;
    memset(flag,0,sizeof(flag));
    for(tx = 0;tx < n;tx ++)
        for(ty = 0;ty < m;ty ++)
            step[tx][ty] = 1000000;
    flag[si][sj] = 1;
    step[si][sj] = 0;
    q.push(ss);
    while(!q.empty())
    {
        now = q.front();
        flag[now.x][now.y] = 0;//同一个节点可以访问多次
        q.pop();
        for(i = 0;i < 4;i ++)
        {
            add = i < 2?p:1;
            int tx = now.x + dx[i];
            int ty = now.y + dy[i];
            if(tx < 0 || ty < 0 || tx >= n || ty >= m || g[tx][ty] == '#' || flag[tx][ty] || step[tx][ty] < step[now.x][now.y] + add)
                continue;
            flag[tx][ty] = 1;
            ss.x = tx;
            ss.y = ty;
            step[tx][ty] = step[now.x][now.y] + add;
            q.push(ss);
        }
    }
    return step[ei][ej];
}

int main()
{
    int i,j,t;
    int cas = 0;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%lf%d",&l,&n);
        getchar();
        for(i = 0;i < n;i ++)
            gets(g[i]);
        m = strlen(g[0]);
        for(i = 0;i < n;i ++)
        {
            for(j = 0;j < m;j++)
            {
                if(g[i][j] == 'S')
                {
                    si = i;
                    sj = j;
                }
                if(g[i][j] == 'E')
                {
                    ei = i;
                    ej = j;
                }
            }
        }
        double ll,rr,tt;
        ll = 0;
        rr = 1000;
        while(rr - ll > eps)
        {
            p = (ll + rr)/2;
            tt = bfs();
            if(tt - l > eps)
                rr = p;
            else
                ll = p;
        }
        printf("Case #%d: ",++ cas);
        printf("%.3lf%%\n",p * 100);
    }
    return 0;
}
//304K	125MS




你可能感兴趣的:(bfs)