POJ 1066 Treasure Hunt 計算幾何 枚舉線段與直線相交

題意 :從邊上出發   找到最少的路徑去取得寶藏    就是開最少了門    計算幾何  枚舉線段與直線相交

枚舉的線段是所有輸入的點 包括牆的四個角的點    直線也一樣     最後找出最小的相交次數   減一就是要開的最少門

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define Max 35
using namespace std;

typedef struct
{
    double x,y;
}point;

point p[Max*2];
int p_len;

typedef struct
{
    point a,b;
}Line;

Line l[Max + 5];
int l_len;

double Multi(point p1, point p2, point p0)//叉積
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

/*直線與線段的相交判斷在線段的端點相交也是相交*/
bool Across(point a, point b, point c, point d)
{
    double tmp = Multi(c, b, a) * Multi(d, b, a);
    if (tmp < 0 || fabs(tmp) < eps) return true;
    return false;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    int T;
    while(scanf("%d",&T) != EOF)
    {
        p[0].x = 0;p[0].y = 0;
        p[1].x = 0;p[1].y = 100;
        p[2].x = 100;p[2].y = 100;
        p[3].x = 100;p[3].y = 0;
        l[0].a = p[0];l[0].b = p[1];
        l[1].a = p[1];l[1].b = p[2];
        l[2].a = p[2];l[2].b = p[3];
        l[3].a = p[3];l[3].b = p[0];
        l_len = 4;
        p_len = 2 * T + 4;
        for(int i = 4; i < 2*T+4; i+=2)
        {
            scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+1].x,&p[i+1].y);
            l[l_len].a = p[i];l[l_len++].b = p[i+1];
        }
        point s;
        scanf("%lf%lf",&s.x,&s.y);
        //枚舉所有點和treasure點組成的線段
        int minn = 1000;
        for(int i = 0; i < p_len; i++)
        {
            int cnt = 0;
            for(int j = 0; j < l_len; j++)//直線與線段的相交次數
            if(Across(l[j].a,l[j].b,p[i],s))cnt++;
            minn = min(minn,cnt);
        }
        printf("Number of doors = %d\n",minn - 1);//因爲是以端點枚舉多了一條相交的
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章