Description
Farmer John likes to do high energy physics experiments on weekends, but the results are counterproductive, resulting in N wormholes on the farm (2<=n<=12,n is even), each at a different point on the farm's two-dimensional map.
According to his calculations, John knew that his wormhole would form a N/2 connection pairing. For example, if the wormhole of A and B are connected to a pair, any object that enters wormhole a will go out from the Wormhole B, in the same direction, and any object that enters wormhole B will also go out from wormhole a, moving in the same direction. This could have had quite unpleasant consequences.
For example, suppose that there are two pairs of wormholes a (3,1) and B (a), and Bessie moves from (2,1) to the +x direction (right). Bessie will enter Wormhole B (in (3,1)), go out from a (in ()), and then enter B again, trapped in an infinite loop!
| . . . .| A > B. Betsy will cross the b,a,+ . And then through B again.
Farmer John knows the exact location of every wormhole in his farm. He knew that Bessie was always walking in the direction of +x, though he could not remember the current position of Bessie. Please help Farmer John calculate different wormhole pairings (conditions) so that Bessie may be trapped in an infinite loop if she starts from a bad position.
[Edit]format
Program NAME: Wormhole
INPUT FORMAT:
(File wormhole.in)
Line 1th: N, Number of wormholes
2nd to n+1: Each row contains two space-delimited integers that describe a single wormhole with (x, y) coordinates. Each coordinate is in the range 0..1000000000.
OUTPUT FORMAT:
(File wormhole.out)
Line 1th: The number of different pairs of cards in the loop that will cause Bessie to move along the +x direction from a certain starting point.
[Edit]SAMPLE INPUT
40 01 01) 10 1
[Edit]SAMPLE OUTPUT
2
[Edit]HINTS[Edit]Enter more information
There are 4 wormholes, at a square corner.
[Edit]Output Details
If we have wormholes numbered 1 to 4, then by matching 1 with 2 and 3 with 4, Bessie will be stuck if she starts from anywhere (0,0) to (1,0) and between (0,1) and (a).
| . . . . 4 3 ... Betsy will cross the B,a,1-2-.-.-. And then through B again.
Similarly, at the same starting point, Bessie will also fall into the loop if the pairing is 1-3 and 2-4.
Only the pairing of 1-4 and 2-3 allows Bessie to go from point to +x direction on any two-dimensional plane without looping.
/* id:twd30651 Prog:wormhole lang:c++*///official answer # include <iostream> #include <fstream>using namespace std;# Define Max_n 12int N, x[max_n+1], y[max_n+1];int partner[max_n+1];int next_on_right[max_n+1];bool cycle_exists (void) {F or (int start=1; start<=n; start++) {//Does there exist a cylce starting from start int pos = start; for (int count=0; count<n; count++) {pos = Next_on_right[partner[pos]]; } if (pos! = 0) return true; } return false;} Count all Solutionsint Solve (void) {//Find first unpaired wormhole int i, total=0; for (I=1; i<=n; i++) if (partner[i] = = 0) break; Everyone paired? if (i > N) {if (cycle_exists ()) return 1; else return 0; }//Try pairing I with any possible other wormholes J for (int j=i+1; j<=n; J + +) if (partner[j] = = 0) { Try pairing I & J, let recursion continue to//generate the rest Of the solution partner[i] = j; PARTNER[J] = i; Total + = solve (); Partner[i] = Partner[j] = 0; } return total;} int main (void) {ifstream fin ("wormhole.in"); Fin >> N; for (int i=1; i<=n; i++) fin >> x[i] >> y[i]; Fin.close (); for (int i=1; i<=n; i++)//Set Next_on_right[i] ... for (int j=1; j<=n; J + +) if (X[j] > X[i] &A mp;& Y[i] = = Y[j])//J Right of I ... if (next_on_right[i] = = 0 | | X[j]-x[i] < X[next_on_right[i]]-x[i]) next_on_right[i] = j; Find the adjacent right parallel point ofstream fout ("Wormhole.out"); Fout << Solve () << "\ n"; Cout<<solve () <<endl; Fout.close (); return 0;}
Usaco 1.3 wormholes (Won't do, post official answer)