学校自己挂的题目。。。我也不知道是哪里的。。。

Broken line
Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

There is a closed broken line on a plane with sides parallel to coordinate axes, without self-crossings and self-contacts. The broken line consists of K segments. You have to determine, whether a given point with coordinates (X0,Y0) is inside this closed broken line, outside or belongs to the broken line.

Input

The first line contains integer K (4 K 10000) - the number of broken line segments. Each of the following N lines contains coordinates of the beginning and end points of the segments (4 integer xi1,yi1,xi2,yi2all numbers in a range from -10000 up to 10000 inclusive). Number separate by a space. The segments are given in random order. Last line contains 2 integers X0 and Y0- the coordinates of the given point delimited by a space. (Numbers X0, Y0in a range from -10000 up to 10000 inclusive).

Output

The first line should contain:

INSIDE - if the point is inside closed broken line,

OUTSIDE - if the point is outside,

BORDER - if the point belongs to broken line.

Sample Input

4
0 0 0 3
3 3 3 0
0 3 3 3
3 0 0 0
2 2

Sample Output

INSIDE

Author : Alex Y. Suslov, Sergey V. Mironov
Resource : 5th Southern Subregional Contest. Saratov 2002
Date : 2002-10-10




题目大意

给出k条平行于x和y轴的线段,再给出一个点的坐标,问这个点是否在坐标内


思路:

看了一下如何判断点在多边形内部的博客。就是根据一点,往两边延伸,如果是都是偶数,那么就是外部,不然是在内部。然后如何这个点延伸出去的线和其中一个的端点有关,那么,我们就只加入这条线段的上端点,而不加入下端点。


#include
#include
#include
#include
#include

using namespace std;

struct point{
    int x, y;
    int x0, y0;
};
point p[10000 + 10];
int nx, ny;
int k;

int solve(){
    int l = 0, r = 0;
    for(int i = 1; i <= k; i++){
        if (nx == p[i].x && ny >= min(p[i].y, p[i].y0) && ny <= max(p[i].y, p[i].y0)) return 0;
        if (ny == p[i].y && nx >= min(p[i].x, p[i].x0) && nx <= max(p[i].x, p[i].x0)) return 0;
    }
    for (int i = 1; i <= k; i++){
        //只看平行于y轴的,也就是说y不相同
        if (p[i].y == p[i].y0) continue;
        if (ny <= min(p[i].y, p[i].y0) || ny > max(p[i].y, p[i].y0)) continue;//夹在两个y的范围之间才能计算
        if (nx > p[i].x){
            l++;
        }
        else if (nx < p[i].x) {
            r++;
        }
    }
    if(l == 0 || r == 0) return 1;
    if (l % 2 == 0 && r % 2 == 0) return 1;
    else return 2;
}

int main(){
    scanf("%d", &k);
    for (int i = 1; i <= k; i++){
        int a, b, c, d;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        if (b > d){
            swap(d, b);
            swap(a, c);
        }
        p[i].x = a, p[i].y = b;
        p[i].x0 = c, p[i].y0 = d;
    }
    scanf("%d%d", &nx, &ny);
    int ty = solve();
    if (ty == 0) printf("BORDER\n");
    else if (ty == 1) printf("OUTSIDE\n");
    else printf("INSIDE\n");
    return 0;
}

/*
8
0 2 1 2
1 2 1 1
1 1 2 1
2 1 2 2
2 2 3 2
3 2 3 0
3 0 0 0
0 0 0 2
4 2
答案 outside

8
0 2 1 2
1 2 1 1
1 1 3 1
3 1 3 2
3 2 4 2
4 2 4 0
4 0 0 0
0 0 0 2
2 2
答案 outside
*/



你可能感兴趣的:(数学)