链接: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 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 

Source

 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
题意是一个金字塔,从中间某点开始往外走,中间会有许多条线代表许多堵墙,你要算如何能尽快出去,并且炸掉最少的墙
一开始没思路,又看了题是每次从各边中间开始炸,还是没思路,各边中间点没法找啊,n条线,形成n个凸多边形,没法
表示。后来想了一个方法,将边界点(整数点)与目标点连线,求最小相交次数,因为只要可以炸掉走出去,必定可以从一条
直线穿出去。枚举所有边界上的点,求最小交点就是解。同时要注意,线段相交端点相交不能算相交,因为相交点可以直接炸开
算一堵墙。
 
然后就是贴个代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h> #define MAXX 35
#define eps 1e-6
using namespace std; typedef struct point
{
double x,y;
} point;
typedef struct line
{
point st,ed;
} line; bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
} bool onSegment(point a,point b,point c)
{
double maxx=max(a.x,b.x);
double maxy=max(a.y,b.y);
double minx=min(a.x,b.x);
double miny=min(a.y,b.y);
if(dd(crossProduct(a,b,c),0.0)&&xyd(c.x,maxx)&&dyd(c.x,minx)
&&xyd(c.y,maxy)&&dyd(c.y,miny))
return true;
return false;
} bool segIntersect(point p1,point p2,point p3,point p4)
{
double d1=crossProduct(p3,p4,p1);
double d2=crossProduct(p3,p4,p2);
double d3=crossProduct(p1,p2,p3);
double d4=crossProduct(p1,p2,p4);
if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
return true;
/*if(dd(d1,0.0)&&onSegment(p3,p4,p1))
return true;
if(dd(d2,0.0)&&onSegment(p3,p4,p2))
return true;
if(dd(d3,0.0)&&onSegment(p1,p2,p3))
return true;
if(dd(d4,0.0)&&onSegment(p1,p2,p4))
return true;
*/
return false;
} point p[10010];
line li[MAXX];
int num[MAXX]; int main()
{
int n,m,i,j;
point tar;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%lf%lf%lf%lf",&li[i].st.x,&li[i].st.y,&li[i].ed.x,&li[i].ed.y);
}
scanf("%lf%lf",&tar.x,&tar.y);
i=0;
int cas=0;
for(j=1; j<100; j++)
{
p[cas].x=0;
p[cas++].y=j;
}
for(j=1; j<100; j++)
{
p[cas].x=100;
p[cas++].y=j;
}
for(i=1; i<100; i++)
{
p[cas].x=i;
p[cas++].y=0;
}
for(i=1; i<100; i++)
{
p[cas].x=i;
p[cas++].y=100;
}
int sum;
int minn=0x7fffffff;//printf("%d**\n",cas);
for(i=0; i<cas; i++)
{
sum=0;
for(j=0; j<n; j++)
{
if(segIntersect(tar,p[i],li[j].st,li[j].ed))
{
sum++;
}
}
if(minn>sum)
{
minn=sum;
}//printf("%d**\n",sum);
}
if(n == 0) printf("Number of doors = 1\n");
else
printf("Number of doors = %d\n",minn+1);
} return 0;
}

  

poj 1066 线段相交的更多相关文章

  1. poj 1269 线段相交/平行

    模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...

  2. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  3. poj 2653 线段相交裸题(解题报告)

    #include<stdio.h> #include<math.h> const double eps=1e-8; int n; int cmp(double x) { if( ...

  4. poj 1410 线段相交判断

    http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  5. Pipe - POJ 1039(线段相交交点)

    题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标.   分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易 ...

  6. Pick-up sticks - POJ 2653 (线段相交)

    题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍.....   分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列, ...

  7. The Doors - POJ 1556 (线段相交)

    题目大意:有一个房间(左上角(0,10),右下角(10,0)),然后房间里有N面墙,每面墙上都有两个门,求出来从初始点(0,5),到达终点(10,5)的最短距离.   分析:很明显根据两点之间直线最短 ...

  8. POJ 2074 | 线段相交

    #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #defi ...

  9. 线段相交 poj 1066

    // 线段相交 poj 1066 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数 // #include <bits/stdc++.h> #include <i ...

随机推荐

  1. android 项目学习随笔十一(ListView下拉刷新提示)

    1. 设置mHeaderView.setPadding TOPPADING为负值,隐藏刷新提示头布局 在onTouchEvent事件中进行头布局显示隐藏切换 import java.text.Simp ...

  2. 怎样使用AutoLayOut为UIScrollView添加约束

    1.在ViewController中拖入1个UIScrollView,并为其添加约束 约束为上下左右四边与superview对齐 2.在scrollview中,拖入1个UIView,为了便于区分将其设 ...

  3. oracle sql语言模糊查询--通配符like的使用教程

    转自:http://www.cnblogs.com/tyler2000/archive/2011/04/28/oracleSql.html oracle在Where子句中,可以对datetime.ch ...

  4. ch2-1:创建模块,并调用模块中的函数

    模块就是一个包含python代码的文本文件,文件名以.py结尾,这是python扩展名: 1.创建一个模块nester.py,文件内容为: '''这是一个模块,可以打印列表,其中可能包含嵌套列表''' ...

  5. composer未升级报错

    错误: Cannot adopt OID in SQUID-MIB: cacheClients ::= { cacheProtoAggregateStats 15 } Cannot adopt OID ...

  6. Java遍历Map的3种方式

    package test; import java.util.Collection; import java.util.HashMap; import java.util.Map; import ja ...

  7. WMsg参数常量值

    //WMsg参数常量值: //创建一个窗口 const int WM_CREATE = 0x01; //当一个窗口被破坏时发送 const int WM_DESTROY = 0x02; //移动一个窗 ...

  8. URL List

    wifi driver http://wenku.baidu.com/view/5fb275e9b8f67c1cfad6b85e.html http://wenku.baidu.com/view/a5 ...

  9. 【转】SVN提示:由于目标机器积极拒绝,无法连接 的解决方法

    转载地址:http://wxiaolei.blog.163.com/blog/static/1038760120133108180596/ 安装完TSVN之后,checkout时报错,并且后来在cmd ...

  10. java环境log4j日志环境的配置。

    首先需要4个jar包.下载地址如下 http://pan.baidu.com/s/1i4k3fiH 期中包含如下包,放到工程的lib中即可.     除此之外还需要一个配置文件,分享链接如下. htt ...