G - Treasure Hunt POJ - 1066

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

这道题目开始的时候就想到建图来做,可是发现图很难建立,因为题目说要从中间打洞才能进如下一个房间,单纯的线段求交很难做。最后看了大佬的题解,才发现无论从正方形的哪一个点进入,只要将进入点与宝藏处连线,这条线段与其余线段交点个数就是要开的门书,根本不用管是不是要从中点处打开。 在网上找了证明也没有看懂,只能记住这个结论了。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define EORR 1e-8
struct point{
  double x;
  double y;
  point(double a = 0, double b = 0) {
    x = a;
    y = b;
  }
};
struct line{
  point s;
  point e;
};
double multi(point p1, point p2, point p3)
{
  return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y);
}
bool Across(line v1, line v2)
{
  if(max(v1.s.x, v1.e.x) >= min(v2.s.x, v2.e.x) &&
     max(v1.s.y, v1.e.y) >= min(v2.s.y, v2.e.y) &&
     max(v2.s.x, v2.e.x) >= min(v1.s.x, v1.e.x) &&
     max(v2.s.y, v2.e.y) >= min(v1.s.y, v1.e.y) &&
     multi(v2.s, v1.e, v1.s)*multi(v1.e, v2.e, v1.s) > 0 &&
     multi(v1.s, v2.e, v2.s)*multi(v2.e, v1.e, v2.s) > 0
  )
  return true;
  return false;
}
int n;
line l[40];
point p;
int main()
{
  cin >> n;
  if(n == 0) {
    printf("Number of doors = 1\n");
    return 0;
  }
  for (int i = 1; i <= n; i++)
  {
    scanf("%lf%lf%lf%lf", &l[i].s.x, &l[i].s.y, &l[i].e.x, &l[i].e.y);
  }
  scanf("%lf%lf", &p.x, &p.y);
  int t = 100000000;
  for (int i = 1; i <= n; i++) {
    line l1, l2;
    int t1, t2;
   t1 = 0;
   t2 = 0;
    l1.s = l[i].s; l1.e = p;
    l2.s = l[i].e; l2.e = p;

    for (int j = 1; j <= n; j++) {
      if(j != i) {
        if(Across(l1, l[j])) t1++;
        if(Across(l2, l[j])) t2++;
      }
    }
    t = min(t, t1);
    t = min(t, t2);
  }
  printf("Number of doors = %d\n", t+1);
  return 0;
}

你可能感兴趣的:(计算几何)