POJ 1066 線段相交

來源:互聯網
上載者:User

題目的意思換句話說:就是要你以線段中的某個點和財寶(treasure)所在點連成一條直線,使得其和其它線段交點數最少;

這樣理解的條件是: 財寶點 不在牆上.題目已給出,滿足;

另外注意的是,初始化最小值肯定是一個很大的值inf; 如果最後得到的是inf,那就置為0(最後為1); 可能很多人錯在這兒

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;#define INF 0x3f3f3f3fconst int maxn = 30+3;#define eps 1e-8struct Point{    float x;    float y;}p[maxn*2],P0;int Sig(float x){    return (x > eps) - (x < -eps);}float 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 line_in_line(Point p1, Point p2, Point p3, Point p4){    return ( Sig(Mult(p1,p3,p2))*Sig(Mult(p1,p4,p2)) < 0 && Sig(Mult(p3,p1,p4))*Sig(Mult(p3,p2,p4)) < 0 );}int main(){#ifndef ONLINE_JUDGE    freopen("in","r",stdin);#endif    int n;    cin>>n;    for(int i = 0; i < n*2; i += 2)    {        cin>>p[i].x>>p[i].y>>p[i+1].x>>p[i+1].y;    }    cin>>P0.x>>P0.y;    int MIN = INF, num = 0;    for(int i = 0; i < n*2; i++)    {        num = 0;        for(int j = 0; j < n*2-1; j += 2)        {            if(line_in_line(P0,p[i],p[j],p[j+1]))                num++;        }//        cout<<i<<" "<<num<<endl;        MIN = num < MIN ? num: MIN;    }    if(MIN == INF )        MIN = 0;    cout<<"Number of doors = "<<MIN+1<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.