[POJ 1066] Treasure Hunt【计算几何】

  • 2017-12-10
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 10000K

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

East Central North America 1999

Solution:

本题有一定思考难度。
首先,从一段墙的任意位置进入均等效,所以可以放弃枚举较难寻找的中点,转而枚举线段的端点
其次,由于线段相交所形成的是凸多边形,所以沿直线突破一定是较优方案,可以连接枚举的端点与目标点,计算与之相交的墙数,取最小值即为答案。
*相交采用不严格相交的判断方式,防止两段墙始于同一点的特殊情况。
**注意特判 n == 0 的情况,直接输出1即可。

Code: O(n2) [84K, 0MS]

// Get the minimum intersection of each endpoint to the destination
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n, ans = 0x3f3f3f3f;

#define Vector Point
struct Point{
	double x, y;
	
	Point() {}
	Point(double x, double y): x(x), y(y) {}
	
	inline Vector operator - (const Point p2) const {return Vector(x - p2.x, y - p2.y);}
} des;

struct Segment{
	Point u, v;
	
	Segment() {}
	Segment(Point u, Point v): u(u), v(v) {}
} w[35];

inline double Cross(const Vector vec1, const Vector vec2) {return vec1.x * vec2.y - vec1.y * vec2.x;}

inline bool is_Intersected(const Segment s1, const Segment s2){
	if(min(s1.u.x, s1.v.x) <= max(s2.u.x, s2.v.x)
	&& max(s1.u.x, s1.v.x) >= min(s2.u.x, s2.v.x)
	&& min(s1.u.y, s1.v.y) <= max(s2.u.y, s2.v.y)
	&& max(s1.u.y, s1.v.y) >= min(s2.u.y, s2.v.y)
	&& Cross(s1.v - s1.u, s2.u - s1.u) * Cross(s2.v - s1.u, s1.v - s1.u) >= 0
	&& Cross(s2.v - s2.u, s1.u - s2.u) * Cross(s1.v - s2.u, s2.v - s2.u) >= 0) return 1;
	return 0;
}

inline int calc(const Point p){
	Segment curs(p, des);
	int cnt = 0;
	for(register int i = 1; i <= n; i++) if(is_Intersected(curs, w[i])) cnt++;
	return cnt;
}

int main(){
	scanf("%d", &n);
	for(register int i = 1; i <= n; i++) scanf("%lf%lf%lf%lf", &w[i].u.x, &w[i].u.y, &w[i].v.x, &w[i].v.y);
	scanf("%lf%lf", &des.x, &des.y);
	if(n == 0){
		printf("Number of doors = 1\n");
		return 0;
	}
	for(register int i = 1; i <= n; i++){
		ans = min(ans, calc(w[i].u));
		ans = min(ans, calc(w[i].v));
	}
	printf("Number of doors = %d\n", ans);
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址:
darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io


常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai