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-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors.
An example is shown below:

Input

The input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).

Output

Print a single line listing the minimum number of doors which need to be created, in the format shown below.

Sample Input

7 
20 0 37 100 
40 0 76 100 
85 0 0 75 
100 90 0 90 
0 71 100 61 
0 14 100 38 
100 47 47 100 
54.5 55.4 

Sample Output

Number of doors = 2 
题意:求从矩形上到宝藏点需要破开的最少的门。。相交点算两张门。
题解:本人方法是,,直接全部枚举,碰到和矩形边相交的直线直接跳过。。最后记得+1
///判断直线与线段相交
///做法:枚举每两个端点,要是存在一条直线经过这两个端点并且和所有线段相交就OK,但是不能为重合点.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 50;
const double eps = 1e-8;
struct Point
{
    double x,y;
};
struct Line
{
    Point a,b;
} line[N];
int n;
double cross(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool isCross(Point a, Point b, Point c, Point d)
{
    if (cross(c, b, a)*cross(b, d, a)<0)return false;
    if (cross(a, d, c)*cross(d, b, c)<0)return false;
    return true;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
        }
        Point e;
        scanf("%lf%lf",&e.x,&e.y);
        int mi = 99999999;
        int cnt;
        for(int j=1; j<=4; j++)
        {
            for(int i=0; i<=100; i++)
            {
                Point s;
                if(j==1) s.x=i,s.y=0;
                if(j==2) s.x=0,s.y=i;
                if(j==3) s.x=100,s.y = i;
                if(j==4) s.x=i,s.y=100;
                cnt=0;
                for(int k=0; k<n; k++){
                    if(fabs(s.x-line[k].a.x)<eps&&fabs(s.y-line[k].a.y)<eps) continue;
                    if(fabs(s.x-line[k].b.x)<eps&&fabs(s.y-line[k].b.y)<eps) continue;
                    if(isCross(s,e,line[k].a,line[k].b)){
                        cnt++;
                    }
                }
                //printf("%d
",cnt);
                if(mi>cnt) mi = cnt;
            }
        }
        if(n==0) printf("Number of doors = 1
");
        else printf("Number of doors = %d
",mi+1);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5448244.html