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

  • 2017-12-10
  • 0
  • 0


Time Limit: 1000MS Memory Limit: 10000K


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:


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


Print a single line listing the minimum number of doors which need to be created, in the format shown below.

Sample Input

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


East Central North America 1999


**注意特判 n == 0 的情况,直接输出1即可。

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

// Get the minimum intersection of each endpoint to the destination
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;






OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.


- Theme by Qzhai