Treasure Hunt Poj1066(线段相交)

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9557 Accepted: 3914

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了)

AC Code

#include<iostream>
	#include<string>
	#include<map>
	#include<algorithm>
	#include<memory.h>
	#include<cmath>
	#include<vector>
	#include<iomanip>
	#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
	using namespace std;
	typedef long long ll;
	const int Max = 1e5 + 5;
	#define EPS (1e-10)
	#define equals(a,b) (fabs((a)-(b))< EPS )

	class Point
	{
   
	public:
		double x, y;
		Point(double x = 0, double y = 0) :x(x), y(y) {
   }
		Point operator + (Point p) {
    return Point(x + p.x, y + p.y); }
		Point operator - (Point p) {
    return Point(x - p.x, y - p.y); }
		Point operator * (double a) {
    return Point(a * x, a * y); }
		Point operator / (double a) {
    return Point(x / a, y / a); }

		double abs() {
    return sqrt(norm()); }
		double norm() {
    return x * x + y * y; }
		bool operator < (const Point& p)const
		{
   
			return x != p.x ? x < p.x : y < p.y;
		}
		bool operator == (const Point& p)const
		{
   
			return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
		}
	};

	typedef Point Vector;


	double cross(Vector a, Vector b)//叉乘,求面积
	{
   
		return a.x * b.y - a.y * b.x;
	}
	double dot(Vector a, Vector b)//点乘,求投影
	{
   
		return a.x * b.x + a.y * b.y;
	}

	int ccw(Point p0, Point p1, Point p2)
	{
   
		Vector a = p1 - p0;
		Vector b = p2 - p0;
		if (cross(a, b) > EPS)return 1;//直线p0-p2在直线p0-p1的逆时针方向;
		if (cross(a, b) < -EPS)return -1;//直线p0-p2在直线p0-p1的顺时针方向;
		if (dot(a, b) < -EPS)return 2;//p2在p0-p1的后方
		if (a.norm() < b.norm())return -2;//p2在p0-p1的前方
		return 0;//p2在p0-p1上
	}

	bool intersect(Point p1, Point p2, Point p3, Point p4)//线段p1p2与线段p3p4是否相交
	{
   
		return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
	}


	Point node[Max];
	int lst[Max], g = 0;
	int main()
	{
   
		FAST;
		int n;cin >> n;
		n *= 2;
		Point oo;
		for (int i = 1;i <= n;i++)cin >> node[i].x >> node[i].y;
		cin >> oo.x>>oo.y;
		int ans = 1e9+5;
		for (int i = 1;i <= n;i++)
		{
   
			int sum = 0;
			for (int j = 1;j < n;j += 2)
			{
   
				if (intersect(node[i], oo, node[j], node[j + 1]))sum++;
			}
			ans = min(ans, sum);
		}
                if(n==0)ans=1;
		cout << "Number of doors = " << ans << endl;
	}
全部评论

相关推荐

头像
05-13 11:19
C++
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务