欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ - 1066 Treasure Hunt 【线段规范相交】

程序员文章站 2024-03-17 10:01:46
...

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:
POJ - 1066 Treasure Hunt 【线段规范相交】
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

题目大意:一个正方形的墓葬内有n堵墙,每堵墙的两个顶点都在正方形的边界上,现在这些墙将墓葬分割成了很多小空间,已知正方形内的一个点上存在宝藏,现在我们要在正方形的外面去得到宝藏,对于每个小空间,我们可以炸开它的任意一条边的中点,现在给出每堵墙的两个节点的坐标和宝藏的坐标,问如果要得到宝藏,需要炸的墙数最少是多少。
枚举正方形边界上的点作为进入正方形的节点,由这个点向宝藏连出一条线段,这条线段和多少个墙相交,那么就需要炸坏多少个墙,找出一个最小的值。
原因,因为每堵墙的两个节点都在边界上,所以如果和墙相交,那么就一定要跨过这道墙,所以需要炸掉它。
一定要是规范相交,因为如果枚举的点如果是一堵墙的节点,那么这堵墙是不应该被计算的。

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
int n,m,pos[5010];
int sgn(double x){
	if(fabs(x)<eps) return 0;
	if(x<0) return -1;	
	else return 1;
}
struct Point{
	double x,y;
	Point(){}
	Point(double _x,double _y){x=_x;y=_y;}
	Point operator -(const Point &b)const{return Point(x - b.x,y - b.y);}
	double operator ^(const Point &b)const{return x*b.y - y*b.x;}
	double operator*(const Point &b)const{return x*b.x+y*b.y;}
	void transXY(double B){
		double tx = x,ty=y;
		x = tx*cos(B)-ty*sin(B);
		y = tx*sin(B)+ty*cos(B);
	}
};
struct Line{
	Point s,e;
	Line(){}
	Line(Point _s,Point _e){s=_s;e=_e;}
	pair<int,Point>operator &(const Line &b)const{
		Point res=s;
		if(sgn((s-e)^(b.s-b.e))==0){
			if(sgn((s-b.e)^(b.s-b.e))==0) return make_pair(0,res);
			else return make_pair(1,res);
		}
		double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
		res.x+=(e.x-s.x)*t;
		res.y+=(e.y-s.y)*t;
		return make_pair(2,res);
	}
};
Line line[100010],lin[100010];
double dist(Point a,Point b){
	return sqrt((a-b)*(a-b));
}
bool inter(Line l1,Line l2){ //判断线段相交  
	return
	max(l1.s.x,l1.e.x)>min(l2.s.x,l2.e.x)&& //这题必须严格相交,所以把=去掉 
	max(l2.s.x,l2.e.x)>min(l1.s.x,l1.e.x)&&
	max(l1.s.y,l1.e.y)>min(l2.s.y,l2.e.y)&&
	max(l2.s.y,l2.e.y)>min(l1.s.y,l1.e.y)&&
	sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0&&
	sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0;
}
int main(){
	int n; scanf("%d",&n);
	for(int i=1;i<=n;i++){
		double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
		line[i].s.x=x1; line[i].s.y=y1;
		line[i].e.x=x2; line[i].e.y=y2;
	} 
	Point p; scanf("%lf%lf",&lin[1].s.x,&lin[1].s.y);
	lin[2]=lin[3]=lin[4]=lin[1];
	lin[1].e.y=lin[4].e.x=100;
	lin[2].e.y=lin[3].e.x=0;
	int ans1=0,ans2=0,ans3=0,ans4=0,ans=n;
	for(int i=1;i<=100;i++){
		lin[1].e.x=lin[2].e.x=lin[3].e.y=lin[4].e.y=i;
		ans1=0,ans2=0,ans3=0,ans4=0;
		for(int j=1;j<=n;j++){
			if(inter(line[j],lin[1])) ans1++;
			if(inter(line[j],lin[2])) ans2++;
			if(inter(line[j],lin[3])) ans3++;
			if(inter(line[j],lin[4])) ans4++;
		}
		ans=min(ans,min(min(ans1,ans2),min(ans3,ans4)));
	}
	ans++;
	printf("Number of doors = %d\n",ans);
	return 0;
}