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. 

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 int n,x[15],y[15];
 7 int p[15];
 8 int ne[15];
 9 bool huan()
10 {
11     for(int i=1;i<=n;i++){//从起点出发,是否有一个环
12         int j=i;
13         for(int c=0;c<n;c++){
14             j=ne[p[j]];
15         }
16         if(j) return true;//n步后如果没环就+1
17     }
18     return false;
19 }
20 //计算所有的可能性
21 int solve()
22 {
23     int i,tot=0;
24     for(i=1;i<=n;i++){//寻找第一个没有连起来的点
25         if(!p[i]) break;
26     }
27 
28     if(i>n){
29         if(huan()) return 1;
30         else return 0;
31     }
32     //将找出来的没配对的点与剩下的点一个个配对
33     for(int j=i+1;j<=n;j++){
34         //将i和j配对
35         //计算剩下的配对情况
36         if(!p[j]){
37             p[i]=j;
38             p[j]=i;
39             tot+=solve();
40             p[i]=p[j]=0;
41         }
42     }
43     return tot;
44 }
45 int main()
46 {
47     freopen("wormhole.in","r",stdin);
48     freopen("wormhole.out","w",stdout);
49     cin>>n;
50     for(int i=1;i<=n;i++) cin>>x[i]>>y[i];
51     for(int i=1;i<=n;i++){
52         for(int j=1;j<=n;j++){
53             if(x[j]>x[i]&&y[i]==y[j]){
54                 if(!ne[i]||x[j]-x[i]<x[ne[i]]-x[i]){
55                     ne[i]=j;
56                 }
57             }
58         }
59     }//找出紧邻的右边平行点
60     cout<<solve()<<endl;
61     return 0;
62 }

 

posted on 2016-01-26 12:28  Sunny糖果  阅读(229)  评论(0编辑  收藏  举报