POJ 3897 Maze Stretching

来源:互联网 发布:软件开发工具研究 编辑:程序博客网 时间:2024/05/16 07:23

题目大意:我们可以对地图的竖向进行压缩或则扩张。

让竖向移动一步的距离由一变成X。最后,问你将地图变成多少比例,

可以让压缩后的地图的最短。题目反正说的好含糊。开始就是直接BFS,找到最短的以后进行压缩。

WA到 我已刻入灵魂。

思路:既然是压缩之后。那么我们就要用二分先求出压缩率,再进行BFS。

然后又会出现这样的情况     

终点     13

12.5

此时的压缩率是300%。所以12.5出列的话答案就是15.5.。但是13出列的话就是14.。所以最优解是13.。这样还是会丢失最优解。

所以就用A*。。。让估价值低的先出列。快哭了

#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <cmath>#define eps 1e-6#define INF 0x3f3f3f3fusing namespace std;int dx[] = {-1,1,0,0};int dy[] = {0,0,-1,1};int v[205][205];int n;double l;struct node{    int x,y;    double step;    double dis;    bool operator < (const node &a)const    {        return dis > a.dis;    }};char map[205][205];int tagx,tagy;bool is_ok(int xx,int yy){    if(xx>=0 && xx<n && yy<strlen(map[xx]) && yy>=0)        return true;    return false;}double getdis(node t,double kp){    return fabs((double)tagx - t.x)*kp + fabs((double)tagy-t.y) + t.step;}double bfs(node t,double kp){    memset(v,0,sizeof(v));    priority_queue<node>Q;    v[t.x][t.y]=0;    node w,e;    Q.push(t);    while(!Q.empty())    {        w=Q.top();        Q.pop();        for(int k=0;k<=3;k++)        {            e=w;            int xx=e.x+dx[k];            int yy=e.y+dy[k];            if(k<=1)            e.step=e.step + kp;            else e.step = e.step + 1;            if(e.step<v[xx][yy] && is_ok(xx,yy) && map[xx][yy]!='#')            {                e.x=xx;                e.y=yy;                e.dis = getdis(e,kp);                v[xx][yy]=e.step;                if(xx==tagx && yy==tagy)                {                    return e.step;                }                Q.push(e);            }        }    }    return 0;}int main(){    node t;    int TT;    scanf("%d",&TT);    int CASE=1;    while(TT--)        {            scanf("%lf%d",&l,&n);            getchar();            for(int i=0;i<n;i++)            {                gets(map[i]);                for(int j=0;j<strlen(map[i]);j++)                {                    if(map[i][j]=='S')                    {                        t.x=i;                        t.y=j;                    }                    else if(map[i][j]=='E')                    {                        tagx=i;                        tagy=j;                    }                }            }            t.step=0;            double left=0,right=10;            double mid;            //printf("bfs = %lf",bfs(t,0.5));            while(right - left > eps)            {                mid=(right + left) / 2;                t.dis=getdis(t,mid);                if(bfs(t,mid)>l)                right=mid;                else if(bfs(t,mid)<l)left= mid;                else break;            }            printf("Case #%d: %.3f%%\n",CASE++,mid*100);        }    return 0;}


原创粉丝点击