线段相交 poj 1066

  1 // 线段相交 poj 1066
  2 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数
  3
  4 // #include <bits/stdc++.h>
  5 #include <iostream>
  6 #include <cstdio>
  7 #include <cstdlib>
  8 #include <algorithm>
  9 #include <vector>
 10 #include <math.h>
 11 using namespace std;
 12 #define LL long long
 13 typedef pair<int,int> pii;
 14 const int inf = 0x3f3f3f3f;
 15 const LL MOD =100000000LL;
 16 const int N =110;
 17 #define clc(a,b) memset(a,b,sizeof(a))
 18 const double eps = 1e-8;
 19 void fre() {freopen("in.txt","r",stdin);}
 20 void freout() {freopen("out.txt","w",stdout);}
 21 inline int read() {int x=0,f=1;char ch=getchar();while(ch>‘9‘||ch<‘0‘) {if(ch==‘-‘) f=-1; ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}return x*f;}
 22
 23 int sgn(double x){
 24     if(fabs(x) < eps)return 0;
 25     if(x < 0)return -1;
 26     else return 1;
 27 }
 28 struct Point{
 29     double x,y;
 30     Point(){}
 31     Point(double _x,double _y){
 32         x = _x;y = _y;
 33     }
 34     Point operator -(const Point &b)const{
 35         return Point(x - b.x,y - b.y);
 36     }
 37     double operator ^(const Point &b)const{
 38         return x*b.y - y*b.x;
 39     }
 40     double operator *(const Point &b)const{
 41         return x*b.x + y*b.y;
 42     }
 43 };
 44
 45 struct Line{
 46     Point s,e;
 47     int inx;
 48     Line(){}
 49     Line(Point _s,Point _e){
 50         s=_s;e=_e;
 51     }
 52 };
 53
 54 Line line[N];
 55 bool inter(Line l1,Line l2){
 56     return
 57         max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
 58         max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
 59         max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
 60         max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
 61         sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
 62         sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
 63 }
 64
 65 vector<Line> list;
 66 bool cmp(Line l1,Line l2){
 67     return l1.inx<l2.inx;
 68 }
 69
 70 Point p[110];
 71 int main(){
 72     int n;
 73     while(~scanf("%d",&n)){
 74     for(int i=1;i<=n;i++){
 75         double x1,y1,x2,y2;
 76         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 77         line[i]=Line(Point(x1,y1),Point(x2,y2));
 78         p[i*2-1]=Point(x1,y1);
 79         p[i*2]=Point(x2,y2);
 80     }
 81     double x1,y1;
 82     scanf("%lf%lf",&x1,&y1);
 83     Point s=Point(x1,y1);
 84     int ans=inf;
 85     for(int i=1;i<=2*n;i++){
 86        int tem=0;
 87        for(int j=1;j<=n;j++){
 88           if(inter(Line(s,p[i]),line[j]))
 89             tem++;
 90        }
 91        ans=min(ans,tem);
 92     }
 93
 94     Point p1;
 95     p1=Point(0,0);
 96     int tem=0;
 97     for(int i=1;i<=n;i++){
 98         if(inter(Line(s,p1),line[i]))
 99           tem++;
100     }
101     ans=min(ans,tem+1);
102
103     p1=Point(0,100);
104     tem=0;
105     for(int i=1;i<=n;i++){
106         if(inter(Line(s,p1),line[i]))
107           tem++;
108     }
109     ans=min(ans,tem+1);
110
111     p1=Point(100,0);
112      tem=0;
113     for(int i=1;i<=n;i++){
114         if(inter(Line(s,p1),line[i]))
115           tem++;
116     }
117     ans=min(ans,tem+1);
118
119     p1=Point(100,100);
120     tem=0;
121     for(int i=1;i<=n;i++){
122         if(inter(Line(s,p1),line[i]))
123           tem++;
124     }
125     ans=min(ans,tem+1);
126     printf("Number of doors = ");
127     printf("%d\n",ans);
128     }
129     return 0;
130 }
时间: 2024-08-26 15:37:10

线段相交 poj 1066的相关文章

简单几何(线段相交) POJ 1066 Treasure Hunt

题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 /************************************************ * Author :Running_Time * Created Time :2015/10/26 星期一 16:30:26 * File Name :POJ_1066.cpp ***********

简单几何(直线与线段相交) POJ 1039 Pipe

题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. /************************************************ * Author :Running_Time * Created Time :2015/10/31 星期六 10:28:12 * File Name :POJ_1039.cpp ***********

简单几何(线段相交) POJ 1410 Intersection

题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /************************************************ * Author :Running_Time * Created Time :2015/10/27 星期二 13:17:49 * File Name :POJ_1410.cpp ******************************************

简单几何(线段相交) POJ 2653 Pick-up sticks

题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /************************************************ * Author :Running_Time * Created Time :2015/10/26 星期一 15:37:36 * File Name :POJ_2653.cpp ************************************************/ #inclu

简单几何(线段相交) POJ 2826 An Easy Problem?!

题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后看看是否高的点覆盖了低的点,用叉积判断方向,其他的情况见网上的解释.貌似没有什么卡精度的数据.最后膜拜楼教主,难以望其项背... /************************************************ * Author :Running_Time * Created Ti

poj 1066 线段相交

链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5431   Accepted: 2246 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid

POJ 1066 Treasure Hunt(线段相交&amp;&amp;转换)

Treasure Hunt 大意:在一个矩形区域内,有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越只能在中点穿越. 思路:需要巧妙的转换一下这个问题,因为从一个点到终点不可能"绕过"围墙,只能穿过去,所以门是否开在中点是无所谓的,只要求四周线段中点到终点的线段与墙的最少交点个数即可.更进一步,实际上,只需判断四周围墙的所有点与终点的连线与内墙的最少交点加一即可. struct Point{ double x, y; } A,

POJ 1066 Treasure Hunt (线段相交)

题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间内可以从房间边界(墙)的中点走过这堵墙,问你从给定的点走到外墙外最少走过的墙数 题解:注意我们可以从每个房间的墙的中点走出,而不是一整条线段(墙)的中点走出.... 然后我们可以找四周的边界中的每个点与给定点的连线,再与给定的线段找相交最少的交点数就是答案 但是边界每个点是无穷多个,因此我们可以这样

poj 1066(枚举+线段相交)

Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6328   Accepted: 2627 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-ar