Algorithm, Data structure/Solved Algorithmic Problem

USACO 1.3 - Wormholes

JaykayChoi 2016. 6. 17. 00:30

Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).

According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.

For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
   | A > B .      Bessie will travel to B then
   + . . . .      A then across to B again

Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.

Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn't know which wormhole pairs with any other wormhole, so find all the possibilities.

PROGRAM NAME: wormhole

INPUT FORMAT:

Line 1:The number of wormholes, N.
Lines 2..1+N:Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT (file wormhole.in):
4
0 0
1 0
1 1
0 1

INPUT DETAILS:

There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT:

Line 1:The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT (file wormhole.out):

2

OUTPUT DETAILS:

If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . . .
   4 3 . . .      Bessie will travel to B then
   1-2-.-.-.      A then across to B again

Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 1-3 and 2-4 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).

Only the pairings 1-4 and 2-3 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling. 



출처: http://train.usaco.org/


N(짝수) 개의 웜홀이 존재한다고 할 때 각 웜홀은 짝을 구성하고 있습니다. 각 pair는 한 쪽으로 들어가면 다른 한쪽으로 나오게 됩니다. +x 로만 이동할 수 있는 Bessie 라는 소가 웜홀에 빠져 무한 cycle 을 돌 수 있는 웜홀의 짝의 종류를 구하는 문제입니다.

재귀 함수를 통해 wormhole 의 pair 을 구성하여 풀었습니다.



my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
 
struct Pos
{
    int x, y;
    Pos(int _x, int _y) : x(_x), y(_y) {}
};
 
int n;
map<intint> wormholeOnRight;
map<intint> pairs;
 
bool isThereInfiniteCycle()
{
    for (int i = 0; i < n; i++)
    {
        bool ret = true;
        int bessie = i;
        for (int j = 0; j < n; j++)
        {
            map<intint>::iterator it = wormholeOnRight.find(pairs[bessie]);
            if (it == wormholeOnRight.end())
            {
                ret = false;
                break;
            }
            else
                bessie = it->second;
        }
        if (ret)
            return true;
    }
    return false;
}
 
int countInfiniteCycle()
{
    if (pairs.size() == n)
    {
        if (isThereInfiniteCycle())
            return 1;
        else
            return 0;
    }
    int ret = 0;
    int firstFree = 0;
    for (int i = 0; i < n; i++)
    {
        map<intint>::iterator it = pairs.find(i);
        if (it == pairs.end())
        {
            firstFree = i;
            break;
        }
    }
 
    for (int pairWith = firstFree + 1; pairWith < n; pairWith++)
    {
        map<intint>::iterator it = pairs.find(pairWith);
        if (it == pairs.end())
        {
            pairs.insert(make_pair(firstFree, pairWith));
            pairs.insert(make_pair(pairWith, firstFree));
            ret += countInfiniteCycle();
 
            pairs.erase(firstFree);
            pairs.erase(pairWith);
        }
    }
 
    return ret;
}
 
int main()
{
    ifstream fin("wormhole.in");
    ofstream fout("wormhole.out");
 
    fin >> n;
    
    vector<Pos> wormholes;
    for (int i = 0; i < n; i++)
    {
        int a, b;
        fin >> a >> b;
        Pos pos(a, b);
        wormholes.push_back(pos);
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (wormholes[i].y == wormholes[j].y && wormholes[j].x > wormholes[i].x)
            {
                map<intint>::iterator it = wormholeOnRight.find(i);
                if (it == wormholeOnRight.end() || wormholes[it->second].x > wormholes[j].x)
                    wormholeOnRight[i] = j;
            }
        }
    }
 
 
    fout << countInfiniteCycle() << endl;
 
    fin.close();
    fout.close();
    return 0;
}
cs