poj3897 Maze Stretching

Maze Stretching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 751   Accepted: 198

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
这题真对精度要求太高了,还是用二分做,好一点,这里,要用三维判重,加一维是从x轴,还是从y轴方向来的才可以,因为,拉伸之后x和y方向是不等价的!之后用个优先队列就好了!
#include <iostream>
#include <stdio.h>
#include <queue>
#define eps 1e-10
#include <math.h>
#include <string.h>
using namespace std;
char str[500];
int map[130][130],visit[130][130][2];
int ex,ey,n,m,sx,sy,tt;
double step,wide,height;
struct maze{
    double step;int x,y;
    bool operator < (maze a) const
    {
        if(a.step<step)//从小到大排
        return true;
        return false;
    }

};
maze temp;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
priority_queue<maze> q;
void init()
{

   int temp,i,j;
   scanf("%lf %d",&step,&n);
   temp=n;
  // getchar();
  gets(str);
   for(j=0;j<n;j++)
   {
       gets(str);
       for(i=0;str[i]!='\0';i++)
       {
           if(str[i]=='#')
            map[j][i]=1;
           else if(str[i]==' ')
           {
               map[j][i]=0;
           }
           else if(str[i]=='S')
           {
               map[j][i]=0;
               sx=j;
               sy=i;

           }
           else if(str[i]=='E')
           {
               map[j][i]=0;
               ex=j;
               ey=i;
           }
       }
      // gets(str);

   }
	m=strlen(str);
	height=1;
    wide=1;
}
bool canbfs(int i)
{
    int x,y,flag;
    if(i<2)
        flag=1;
    else
        flag=0;
    x=temp.x+dir[i][0];
    y=temp.y+dir[i][1];
    if(x>=0&&x<n&&y>=0&&y<m&&(!map[x][y])&&!visit[x][y][flag])
    {
        visit[x][y][flag]=1;
        return true;
    }
    return false;
}
int bfs()
{
    int i;
    memset(visit,0,sizeof(visit));
    while(!q.empty())
    {
        q.pop();
    }
	maze prime;
    temp.step=0;
    temp.x=sx;
    temp.y=sy;
    q.push(temp);
	visit[sx][sy][1]=1;//起点标记为已知
	visit[sx][sy][0]=1;
    while(!q.empty())
    {
        prime=q.top();
        q.pop();
        if(prime.x==ex&&prime.y==ey)
        {
            //printf("step%.10f\n",prime.step);
            if(fabs(prime.step-step)<=0.00000001)//达到要求
            {
                return  2;
            }
            else if(prime.step>=step)//比要求的大
            {
                return 1;
            }
            else if(prime.step<step)//返回小
			    return 0;
        }
        for(i=0;i<4;i++)
        {
			temp=prime;
            if(canbfs(i))
            {
                temp.x=temp.x+dir[i][0];
                temp.y=temp.y+dir[i][1];
                //visit[temp.x][temp.y]=1;
                if(i<=1)
                temp.step=temp.step+height;
                else
                temp.step+=wide;
                q.push(temp);
            }
        }
    }
    return 0;
}
int twocut()
{
    double s,e,mid;
    s=0;e=10005;
    int re;
    while(e-s>=eps)
    {
        mid=(s+e)/2.0;
        height=mid;
        re=bfs();
		//printf("%.10f %d\n",mid,re);
        if(re==0)//放小了
        {
            s=mid;
        }
        else if(re==1)//放大了
        {
            e=mid;

        }
        else
        {//找到
            printf("Case #%d: %.3f%%\n",tt,100*mid);
            return 1;
        }
    }
// printf("Case #%d: %.3f%%\n",tt,100*mid);
return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    for(tt=1;tt<=t;tt++)
    {
		init();
        twocut();//二分查找

    }
    return 0;
}



你可能感兴趣的:(poj3897 Maze Stretching)