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;
	}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 13:47
点赞 评论 收藏
分享
投递长鑫存储等公司7个岗位
点赞 评论 收藏
分享
05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
投递美团等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务