码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1066(枚举+线段相交)

时间:2016-04-30 11:15:59      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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\n",cnt);
                if(mi>cnt) mi = cnt;
            }
        }
        if(n==0) printf("Number of doors = 1\n");
        else printf("Number of doors = %d\n",mi+1);
    }

    return 0;
}

 

poj 1066(枚举+线段相交)

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5448244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!