POJ1556 计算几何+Dijkstra(邻接矩阵)

The Doors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9006 Accepted: 3456

Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows. 


4 2 7 8 9 
7 3 4.5 6 7 

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1. 

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

Source

题意:
给出一个10*10的方格,与n(n<=18)道墙,求(0,5)到(10,5)的最短路。(墙的顺序是给定的,无需排序)

解题思路:

这是个计算几何+最短路问题无疑。由向量的叉积判断线段相交可以判断哪里能走。

开始的时候,由于看着数据比较小(80*4),所以我就用BFS+优先队列写了一份代码,样例过了就直接提交了,结果MLE。很是不解,仔细思考后发现,由于不知道下一步能走到何处,所以对于每一个节点都要往后的每个点进行判断,不免会发现假设后面的所有的节点都可以入队,那么队列肯定会炸(阶乘!)

后来,我就用dijkstra算法写,又担心建立邻接矩阵的时候会TLE,还是抱着试一试的心态写了。我创建邻接矩阵算法复杂度确实很大,我是枚举两个点,然后再枚举他们中间的所有的墙(感觉这里可以剪枝),依次判断(想想复杂度就上去了,汗。。)。写完后,样例过了就提交,结果AC了。以下是我的AC代码

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 20
#define inf 0x3f3f3f3f

struct wall
{
    double x,y[4];
} arr[maxn];
double pic[maxn*4][maxn*4];
double road[maxn][4];
int t;

int check(wall a,double x1,double y1,double x2,double y2)//每道墙有两处空白,即可以走的路,只需要两点与空白有交点就可以走,不然要判断三次
{
    if(((a.x-x1)*(a.y[1]-y2)-(a.x-x2)*(a.y[1]-y1))*((a.x-x1)*(a.y[0]-y2)-(a.x-x2)*(a.y[0]-y1))<=0) return 1;
    if(((a.x-x1)*(a.y[3]-y2)-(a.x-x2)*(a.y[3]-y1))*((a.x-x1)*(a.y[2]-y2)-(a.x-x2)*(a.y[2]-y1))<=0) return 1;
    return 0;
}
void init()
{
    arr[0].x=0;
    arr[t+1].x=10;

    for(int i=0; i<4; i++)
        arr[0].y[i]=arr[t+1].y[i]=5;
    for(int i=1; i<=t; i++)
        cin>>arr[i].x>>arr[i].y[0]>>arr[i].y[1]>>arr[i].y[2]>>arr[i].y[3];

    for(int i=0; i<maxn*4; i++)
        for(int j=0; j<maxn*4; j++)
            pic[i][j]=inf;
    for(int i=0; i<maxn; i++)
        for(int j=0; j<4; j++)
            road[i][j]=inf;
}
void dijkstra()
{
    for(int i=0; i<=t+1; i++)
        for(int j=0; j<4; j++)
            for(int a=i+1; a<=t+1; a++)
                for(int b=0; b<4; b++)
                {
                    int flag=1;
                    for(int w=i+1; w<=a; w++)
                    {
                        if(!check(arr[w],arr[i].x,arr[i].y[j],arr[a].x,arr[a].y[b])) flag=0;
                    }
                    if(flag) pic[i*4+j][a*4+b]=sqrt((arr[i].x-arr[a].x)*(arr[i].x-arr[a].x)+(arr[i].y[j]-arr[a].y[b])*(arr[i].y[j]-arr[a].y[b]));
                }
    road[0][0]=road[0][1]=road[0][2]=road[0][3]=0;
    for(int i=0; i<maxn*4; i++)
        for(int j=0; j<maxn*4; j++)
        {
            if(pic[i][j]!=inf)
            {
                road[j/4][j%4]=min(road[j/4][j%4],road[i/4][i%4]+pic[i][j]);
            }
        }
    printf("%.2f\n",road[t+1][0]);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&t)&&t!=-1)
    {
        if(t==0)
        {
            printf("10.00\n");
            continue;
        }
        init();
        dijkstra();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值