POJ 1066--Treasure Hunt(判断线段相交)

2019-02-20 10:42:57 浏览数 (2)

Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 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. 题意:在一个100*100的矩形中存在着条端点在边界上的墙,最多只能有2面墙交在同一点上,在其中有一个目标点,问从边界外进入内部到达目标点,需要开多少面墙,爆破墙面只能在墙的中点上
  2. 我只想说,审题很重要,一开始把爆破墙面只能在墙的中点上理解成端点在边界上的墙的中点爆破而不是由各个墙面分割而成的相对更小的墙面,觉得很难处理Orz. ,看了discuss自己想岔了,实际是判断线段相交的问题,用连接端点和目标点的线段去判断相交,所得到的最小相交数 1就是答案了。而且枚举端点和枚举相邻端点的中点是一样,因为只要是从这两个相邻端点形成的墙面进去内部,内部还是一个封闭的,无法绕过墙来减少相交,所以相交数还是一样的。 最外围的围墙爆破的墙面也要算进去,所以 1.
  3. 代码如下
代码语言:javascript复制
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAX = 1005;
const double eps = 1e-8;
typedef struct point {
    double x;
    double y;
    point() {

    }
    point(double a, double b) {
        x = a;
        y = b;
    }
}point;
typedef struct edge {
    point start;
    point end;
    edge() {

    }
    edge(point a, point b) {
        start = a;
        end = b;
    }
}edge;

int n;
double x, y, x11, y11, x2, y2;
point p1[MAX], p2[MAX],tg,tem;//两个端点
edge line[MAX], ed;
point know[4]{
    {0,0},
    {0,100},
    {100,0},
    {100,100}
};
double multi(point p1, point p2, point p0) { //叉积
    return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y);
}

inline double max(double a, double b) { return a > b ? a : b; }
inline double min(double a, double b) { return a < b ? a : b; }
bool Across(edge v1, edge v2) { //线段相交
    if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) &&
        max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) &&
        max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) &&
        max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) &&
        multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v1.start) > 0 &&
        multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v2.start) > 0
        )
        return true;
    return false;
}


int main() {
    cin >> n;
    if (n == 0) {
        cout << "Number of doors = 1" << endl;
        return 0;
    }
    for (int i = 0; i < n; i  ) {  
        cin >> x11 >> y11 >> x2 >> y2;
        p1[i].x = x11, p1[i].y = y11;  //一个端点
        p2[i].x = x2, p2[i].y = y2;     //另一个端点
        line[i].start = p1[i];
        line[i].end = p2[i];
    }
    cin >> x >> y;
    tg.x = x, tg.y = y;
    int ans = 0xffffff;
    for (int i = 0; i < n; i  ) {
        ed.start = tg, ed.end = p1[i];      //目标点和其中一个端点连线
        int cnt = 1;                        //最外围的围墙也要算
        for (int j = 0; j < n; j  ) {
            if (Across(ed,line[j]))
                cnt  ;
        }
        ans = min(ans, cnt);
    }
    for (int i = 0; i < n; i  ) {
        ed.start = tg, ed.end = p2[i];      //目标点和其中一个端点连线
        int cnt = 1;
        for (int j = 0; j < n; j  ) {
            if (Across(ed, line[j]))
                cnt  ;
        }
        ans = min(ans, cnt);
    }
    for (int i = 0; i < 4; i  ) {
        ed.start = tg, ed.end = know[i];    //目标点和已知端点连线
        int cnt = 1;
        for (int j = 0; j < n; j  ) {
            if (Across(ed, line[j]))
                cnt  ;
        }
        ans = min(ans, cnt);
    }
    cout << "Number of doors = " <<ans<< endl;
    return 0;
}

0 人点赞