POJ 3304 判斷線段 與 直線 相交

來源:互聯網
上載者:User

題意:給出n條線段,判斷是否存在有一條直線,滿足所有的線段在直線上投影后至少有一個公用點(與所有線段都會相交)

開始想錯了,因為是再已經給的線段中 是否存在選一條做直線,使得它與所有線段相交;

這樣也能過sample input;

害我找了一天了;

#include <iostream>#include <cmath>#include <cstdlib>#include <cstdio>using namespace std;const double eps = 1e-8;const int maxn = 100+5;struct Point{    double x;    double y;    Point(double a = 0, double b = 0){ x = a; y = b; }} P[maxn*2];struct Line{    Point u;    Point v;} L;int T,N;int Sig(double x)  {    return (x > eps) - (x < -eps);//    return x < -eps? -1 : x > eps;  //這個也行}double Mult(Point p1, Point p2)  // 兩點 叉積{    return p1.x*p2.y - p1.y*p2.x;}double Mult(Point p0, Point p1, Point p2)  //三點 叉積{    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);}int same_site(Point p1, Point p2, Line L)  //point p1 & p2 on the same site of line L{    int a = Sig(Mult(L.u,L.v,p1));    int b = Sig(Mult(L.u,L.v,p2));    return a*b > 0;}int Judge(Line L){    if(Sig(L.u.x - L.v.x)==0 && Sig(L.u.y - L.v.y)==0)        return false;    for(int i = 1; i < 2*N; i += 2)        if(same_site(P[i],P[i+1],L))            return false;    return true;}int slove(){    for(int i = 1; i < N*2; i++)    {        for(int j = i+1; j <= N*2; j++)        {            L.u.x = P[i].x; L.u.y = P[i].y;            L.v.x = P[j].x; L.v.y = P[j].y;            if(Judge(L))//如果成功,即找到了這條直線                return true;        }    }    return false;}int main(){#ifndef ONLINE_JUDGE    freopen("in","r",stdin);#endif    cin>>T;    while(T--)    {        cin>>N;        for(int i = 1; i <= N*2; i++)        {            cin>>P[i].x>>P[i].y;        }        if(slove())            puts("Yes!");        else            puts("No!");    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.