POJ 1066 Treasure Hunt

题目大意:求到到目标点至少需要穿过几道墙。

题目思路:暴力循环,计算各个点(不要忘记四角)与目标点的连线穿过多少条线段,取最小值。

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define MAXSIZE 100005
#define INF 0x3f3f3f3f
#define LL long long

using namespace std;

//求到到目标点至少需要穿过几道墙。

struct node
{
    double x1,y1,x2,y2;
} well[MAXSIZE];

struct node1
{
    double x,y;
} p[MAXSIZE];

double Cross(double x1,double y1,double x2,double y2,double x3,double y3)
{
    return (x1-x2)*(y2-y3)-(x2-x3)*(y1-y2);
}

void Solve(int n,int cns,double ex,double ey)
{
    int minn=INF,sum;
    for(int i=1; i<cns; i++)
    {
        sum=0;
        for(int j=1; j<=n; j++)
        {
            double op1=Cross(p[i].x,p[i].y,well[j].x1,well[j].y1,well[j].x2,well[j].y2)
                        *Cross(ex,ey,well[j].x1,well[j].y1,well[j].x2,well[j].y2);
            double op2=Cross(well[j].x1,well[j].y1,p[i].x,p[i].y,ex,ey)
                        *Cross(well[j].x2,well[j].y2,p[i].x,p[i].y,ex,ey);
            if(op1<0 && op2<0)
                sum++;
        }
        minn=min(minn,sum);
    }
    printf("Number of doors = %d\n",minn+1);
}

int main()
{
    int n,cns=1;
    double x1,y1,x2,y2,ex,ey;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            well[i].x1=x1;
            well[i].y1=y1;
            well[i].x2=x2;
            well[i].y2=y2;
            p[cns].x=x1;
            p[cns++].y=y1;
            p[cns].x=x2;
            p[cns++].y=y2;
        }

        p[cns].x=0;
        p[cns++].y=0;
        p[cns].x=0;
        p[cns++].y=100;
        p[cns].x=100;
        p[cns++].y=100;
        p[cns].x=100;
        p[cns++].y=0;

        scanf("%lf%lf",&ex,&ey);
        Solve(n,cns,ex,ey);
    }
    return 0;
}
View Code

 

posted @ 2016-10-31 15:33  声声醉如兰  阅读(206)  评论(0编辑  收藏  举报