码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 1.3 Wormholes

时间:2015-11-27 10:44:55      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

Wormholes

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. 

 

题目大意:一个农场,上面有N个虫洞(N是偶数),虫洞两两链接,从一端进入会从另一端出来,出来的时候方向不改变。有的虫洞可能会造成“无限循环”,比如1和2在同一直线上,2在1右边,2和1链接,从1出发向右走,到达2后又会传送到1,又到达2,传送到1,永远走不出来(行走方向恒向右),现在给出n个虫洞的坐标,问有多少种虫洞匹配方式可以造成至少一个“无限循环”。

(在样例里面,12匹配,34匹配,可以形成两个无限循环,但是对答案的贡献为一。)

 

一开始就在寻找通式,想了半小时,有点思路(以为是个DP+数论的题)准备写代码,去确认数据范围才发现发现n居然只有12,那不是各种暴力么。。。。。。

思考前确认数据范围

思考前确认数据范围

思考前确认数据范围

首先确定了一种策略,保证每种匹配都会经历且只经历一次,就是先从12个点中挑出6个点(C 12,6),再对剩下的点全排列(A 6 6),与保留的点一一匹配,且必须保证每一对匹配,保留的点的编号小于挑选的点的编号(因为反过来是一样的,避免重复)。

匹配完之后check一次,看看有没有从任何一个点出发会陷入“无限循环”,如果有,ans++。看起开思路是无懈可击的(而且很简单),可是就是没过(跪在了第九组),先贴上来,一会儿对匹配策略修改下。

 

  1 /*
  2 ID:fffgrdcc1
  3 LANG:C++
  4 TASK:wormhole
  5 */
  6 //12个里面选6个,剩下6个全排列。
  7 #include<cstdio>
  8 #include<iostream>
  9 #include<cstring>
 10 #include<algorithm>
 11 using namespace std;
 12 int n;
 13 struct str
 14 {
 15     int x,y;
 16 }e[13];
 17 //bool vis[12]={0};
 18 int matcha[6],matchb[6];
 19 bool kong(str x,str y)
 20 {
 21     return (x.y<y.y||(x.y==y.y&&x.x<y.x));
 22 }
 23 int jiecheng[6]={1,2,6,24,120,720};
 24 int afterr[13],next[13]={0},ans=0,tota=0,totb=0;
 25 bool check()
 26 {
 27     //int tot=0;
 28     int flag=0;
 29     for(int i=0;i<n&&!flag;i++)
 30     {
 31         int tot=0;
 32         int nown=i;
 33         while(nown)
 34         {
 35             tot++;
 36             nown=next[nown];
 37             //if(nown==0)
 38             nown=afterr[nown];
 39             if(tot>n+1)
 40             {
 41                 flag=1;
 42                 break;
 43             }
 44         }
 45     }
 46     return flag;
 47 }
 48 void work()
 49 {
 50     sort(matchb,matchb+n/2);
 51     //printf("%d %d\n",tota,totb);
 52     for(int i=0;i<jiecheng[n/2-1];i++)
 53     {
 54         int flag=0;
 55         for(int j=0;    j<n/2;j++)
 56         {
 57             if(matcha[j]>matchb[j])
 58             {
 59                 flag=1;
 60                 break;
 61             }
 62         }
 63         if(flag)
 64         {
 65             next_permutation(matchb,matchb+n/2);
 66             continue;
 67         }
 68         /*
 69         for(int j=0;j<6;j++)
 70             printf("%d ",matcha[j]);printf("\n");
 71         for(int j=0;j<6;j++)
 72             printf("%d ",matchb[j]);printf("\n");printf("\n");
 73         */
 74         for(int j=0;j<n/2;j++)
 75         {
 76             next[matchb[j]]=matcha[j];
 77             next[matcha[j]]=matchb[j];
 78         }
 79         if(check())ans++;
 80         next_permutation(matchb,matchb+n/2);
 81     }
 82     return ;
 83 }
 84 void dfs(int v)
 85 {
 86     if(tota==totb&&tota==n/2)
 87     {
 88         work();
 89     }
 90     if(tota<n/2)
 91     {
 92         matcha[tota++]=v;
 93         dfs(v+1);
 94         tota--;
 95     }
 96     if(totb<n/2)
 97     {
 98         matchb[totb++]=v;
 99         //vis[v]=1;
100         dfs(v+1);
101         totb--;
102     }
103     return ;
104 }
105 int main()
106 {
107     //freopen("wormhole.in","r",stdin);
108     //freopen("wormhole.out","w",stdout);
109     scanf("%d",&n);
110     for(int i=1;i<=n;i++)
111     {
112         scanf("%d%d",&e[i].x,&e[i].y);
113     }
114     sort(e+1,e+n+1,kong);
115     for(int i=1;i<n;i++)
116     {
117         if(e[i].y==e[i+1].y)afterr[i]=i+1;
118     }
119     dfs(1);
120     printf("%d\n",ans);
121     return 0;
122 }

附上卡我的数据

Here are the respective outputs:
        ----- our output ---------
        7350
        ---- your output ---------
        7005
        --------------------------

        ------ Data for Run 9 [length=243 bytes] ------
        12 
        572085931 667578536 
        964406504 667578536 
        656852339 870264627 
        110654368 823223484 
        513786208 528178006 
        620147001 528178006 
        227047539 667578536 
        656852339 528178006 
        945298921 528178006 
        945298921 870264627 
        840030425 870264627 
        828839382 528178006 
        ----------------------------

 

 

 

 

 

 

下面的思路是这样的,12个里面选1号,然后在剩下的里面随机选择一个和它组成一个匹配,剩下的里面选择编号最小的,再随机选一个和他匹配,重复该过程直到选完所有的点。复杂度低于上面这个?最大种数为:11*9*7*5*3*1=10^4;

 

  1 /*
  2 ID:fffgrdcc1
  3 LANG:C++
  4 TASK:wormhole
  5 */
  6 //12个里面选6个,剩下6个全排列。
  7 #include<cstdio>
  8 #include<iostream>
  9 #include<cstring>
 10 #include<algorithm>
 11 using namespace std;
 12 int n;
 13 struct str
 14 {
 15     int x,y;
 16 }e[13];
 17 int vis[13]={0};
 18 //bool vis[12]={0};
 19 int matcha[6],matchb[6];
 20 bool kong(str x,str y)
 21 {
 22     return (x.y<y.y||(x.y==y.y&&x.x<y.x));
 23 }
 24 int jiecheng[6]={1,2,6,24,120,720};
 25 int afterr[13],next[13]={0},ans=0,tota=0,totb=0;
 26 bool check()
 27 {
 28     //int tot=0;
 29     int flag=0;
 30     for(int i=0;i<n&&!flag;i++)
 31     {
 32         int tot=0;
 33         int nown=i;
 34         while(nown)
 35         {
 36             tot++;
 37             nown=next[nown];
 38             //if(nown==0)
 39             nown=afterr[nown];
 40             if(tot>n+1)
 41             {
 42                 flag=1;
 43                 break;
 44             }
 45         }
 46     }
 47     return flag;
 48 }
 49 void work()
 50 {
 51     for(int j=0;j<n/2;j++)
 52     {
 53         next[matchb[j]]=matcha[j];
 54         next[matcha[j]]=matchb[j];
 55     }
 56     if(check())ans++;
 57     return ;
 58 }
 59 void dfs(int v)
 60 {
 61     if(tota==totb&&tota==n/2)
 62     {
 63         work();
 64     }
 65     if(v==7)return ;
 66     int minn=100;
 67     for(int i=1;i<=n;i++)
 68     {
 69         if(!vis[i])
 70         {
 71             minn=i;
 72             matcha[tota++]=i;
 73             vis[i]=1;
 74             break;
 75         }
 76     }
 77     for(int i=1;i<=n;i++)
 78     {
 79         if(!vis[i])
 80         {
 81             matchb[totb++]=i;
 82             vis[i]=1;
 83             dfs(v+1);
 84             vis[i]=0;
 85             totb--;
 86         }
 87     }
 88     vis[minn]=0;
 89     tota--;
 90 }
 91 int main()
 92 {
 93     freopen("wormhole.in","r",stdin);
 94     //freopen("wormhole.out","w",stdout);
 95     scanf("%d",&n);
 96     for(int i=1;i<=n;i++)
 97     {
 98         scanf("%d%d",&e[i].x,&e[i].y);
 99     }
100     sort(e+1,e+n+1,kong);
101     for(int i=1;i<n;i++)
102     {
103         if(e[i].y==e[i+1].y)afterr[i]=i+1;
104     }
105     dfs(1);
106     printf("%d\n",ans);
107     return 0;
108 }

惊了。。。还是7005,答案应该是7350的。。。。这让我不得不思考是不是我的check函数跪了。。。

USACO 1.3 Wormholes

标签:

原文地址:http://www.cnblogs.com/xuwangzihao/p/4999769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!