[UVA][射線法、線段交] 10348 - Submarines@Morris' Blog|PChome Online 個人新聞台
24h購物| | PChome| 登入
2013-03-16 19:10:10| 人氣472| 回應0 | 上一篇 | 下一篇

[UVA][射線法、線段交] 10348 - Submarines

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D

Submarines

Input: standard input

Output: standard output

Time Limit: 5 seconds

Memory Limit: 32 MB

 

The Russian navy has, as you probably know, had some problems with their submarines in Swedish waters. These problems are very embarrassing for the Russian military, who has decided to do something about it.

 

As a first step in their plan, they want to construct a program for their centre of command to be able to locate their submarines. From GPSes (Global Positioning Systems) they are able to get X and Y coordinates for the front and rear points of all submarines. The Russian intelligence has in addition provided data for all islands in the Swedish waters. With this information, and with a lot of help from simulations using GPS equipped minks, Russian programmers have been able to develop a program that, from the information given, tells the user if a submarine is

 

1. Still in water

2. Partially on land

3. Entirely on Land

 

The Input

• First you will get the number of submarines that are present in the area. [1..100]

• After that, for each submarine, you will get the X and Y positions first for the submarines    

  front and then for its rear. [-1000..1000]

• Then you will get the number of islands in the area. [1..50]

  After that, for each island you will get the following:

• The number of points of the polygon that represents the island. [3..20]

• The X and Y positions of the points. [-1000..1000].

 

Input rules

All input are integers.

No submarine end point will be on an island edge.

No submarine will lie on an island edge.

All islands are simple polygons without crossing edges or touching borders.

The first point of the island is connected to the last point.

All submarines are approximated with a line between the front and rear points.

Two islands never overlap or touch each other.

 

The Output

Output contains information about every submarine, in order.

If the submarine is all in water, print “Submarine X is still in water.”

If the submarine is all on land, print “Submarine X is completely on land.”

If the submarine is partially in water and partially on land, print “Submarine X is partially on land.”

X is the submarine’s number (1, 2, 3…) according to its order in the input file.

Sample Input (See image)

5

11 11 9 9

0 0 4 0

0 1 10 1

0 6 6 0

0 -10 0 -9

2

4

-5 -5

5 -5

5 5

-5 5

3

-2 -8

-1 -9

-2 -10

Output for Sample Input

Submarine 1 is still in water.

Submarine 2 is completely on land.

Submarine 3 is partially on land.

Submarine 4 is partially on land.

Submarine 5 is still in water.


Swedish National Contest

很容易忘記的情況,如下。

tricky test case

1
1 3 5 3
1
8
0 0
0 4
2 4
2 2
4 2
4 4
6 4
6 0

基本上就沒有什麼好講的了。

#include <stdio.h>
struct Pt {
    double x, y;
};
struct seg {
    Pt s, e;
};
struct polygon {
    int n;
    Pt p[50];
};
int inPolygon(Pt q, polygon &p) {
    int i, j, cnt = 0;
    for(i = 0, j = p.n-1; i < p.n; j = i++) {
        if(p.p[i].y > q.y != p.p[j].y > q.y &&
           q.x < (p.p[j].x-p.p[i].x)*(q.y-p.p[i].y)/(p.p[j].y-p.p[i].y) + p.p[i].x)
           cnt++;
    }
    return cnt&1;
}
double cross(Pt o, Pt a, Pt b) {
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int intersection(Pt as, Pt at, Pt bs, Pt bt) {
    if(cross(as, at, bs)*cross(as, at, bt) < 0 &&
       cross(at, as, bs)*cross(at, as, bt) < 0 &&
       cross(bs, bt, as)*cross(bs, bt, at) < 0 &&
       cross(bt, bs, as)*cross(bt, bs, at) < 0)
        return 1;
    return 0;
}
int main() {
    seg s[150];
    polygon poly[50];
    int n, m, i, j, k, kk;
    while(scanf("%d", &n) == 1) {
        for(i = 0; i < n; i++)
            scanf("%lf %lf %lf %lf", &s[i].s.x, &s[i].s.y,
                        &s[i].e.x, &s[i].e.y);
        scanf("%d", &m);
        for(i = 0; i < m; i++) {
            scanf("%d", &poly[i].n);
            for(j = 0; j < poly[i].n; j++)
                scanf("%lf %lf", &poly[i].p[j].x, &poly[i].p[j].y);
        }
        for(i = 0; i < n; i++) {
            int flag = 0;
            for(j = 0; j < m; j++) {
                int l, r;
                l = inPolygon(s[i].s, poly[j]);
                r = inPolygon(s[i].e, poly[j]);
                if(l != r) //Partially on land
                    flag = 1;
                if(l == 1 && r == 1) {
                    flag = 2;
                    for(k = 0, kk = poly[j].n-1; k < poly[j].n; kk = k++) {
                        if(intersection(poly[j].p[k], poly[j].p[kk], s[i].s, s[i].e))
                            flag = 1;
                    }
                }
                if(flag)    break;
                l = 0, r = 0;
                for(k = 0, kk = poly[j].n-1; k < poly[j].n; kk = k++) {
                    if(intersection(poly[j].p[k], poly[j].p[kk], s[i].s, s[i].e))
                         l = 1;
                }
                if(l)
                    flag = 1;
                if(flag)    break;
            }
            printf("Submarine %d is ", i+1);
            if(!flag)
                puts("still in water.");
            else if(flag == 1)
                puts("partially on land.");
            else
                puts("completely on land.");
        }
        break;
    }
    return 0;
}

台長: Morris
人氣(472) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二分答案] 11860 - Document Analyzer
此分類上一篇:[UVA][大數] 11113 - Continuous Fractions

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文