zoukankan      html  css  js  c++  java
  • USACO 1.3 Wormholes

    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
    ",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("
    ");
     71         for(int j=0;j<6;j++)
     72             printf("%d ",matchb[j]);printf("
    ");printf("
    ");
     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
    ",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
    ",ans);
    107     return 0;
    108 }

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

    总算过掉了。。。注意上面第30行,正确的写法应该从1到n

    边界的重要性

    ,改完后第九组果然过了,开开心心的提交,结果第一组跪了。。。。注意第70行,应该把7改为n/2+1

    提交前检查样例!

    不要用特殊情况,随时保证自己的程序写的是普适算法

    正确代码如下。。。

      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 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 int partner[13];
     26 bool check()
     27 {
     28     //int tot=0;
     29     int flag=0;
     30     for(int i=1;i<=n&&!flag;i++)
     31     {
     32         int tot=0;
     33         int nown=i;
     34         while(nown)
     35         {
     36             tot++;
     37             nown=next[nown];
     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     for(int j=0;j<n/2;j++)
     51     {
     52         next[matchb[j]]=matcha[j];
     53         next[matcha[j]]=matchb[j];
     54     }
     55     if(check())ans++;
     56     return ;
     57 }
     58 void dfs(int v)
     59 {
     60     if(tota==totb&&tota==n/2)
     61     {
     62         work();
     63     }
     64     if(v==n/2+1)return ;
     65     int minn=100;
     66     for(int i=1;i<=n;i++)
     67     {
     68         if(!vis[i])
     69         {
     70             minn=i;
     71             matcha[tota++]=i;
     72             vis[i]=1;
     73             break;
     74         }
     75     }
     76     for(int i=1;i<=n;i++)
     77     {
     78         if(!vis[i])
     79         {
     80             matchb[totb++]=i;
     81             vis[i]=1;
     82             dfs(v+1);
     83             vis[i]=0;
     84             totb--;
     85         }
     86     }
     87     vis[minn]=0;
     88     tota--;
     89 }
     90 int main()
     91 {
     92     freopen("wormhole.in","r",stdin);
     93     freopen("wormhole.out","w",stdout);
     94     scanf("%d",&n);
     95     for(int i=1;i<=n;i++)
     96     {
     97         scanf("%d%d",&e[i].x,&e[i].y);
     98     }
     99     sort(e+1,e+n+1,kong);
    100     for(int i=1;i<n;i++)
    101     {
    102         if(e[i].y==e[i+1].y)afterr[i]=i+1;
    103     }
    104     dfs(1);
    105     printf("%d
    ",ans);
    106     return 0;
    107 }

    顺便附上传说中的官方标程,写的和我第二次的思路一样(第一次的思路也没错),但是比我的简洁很多

     1 /*
     2   ID:twd30651
     3   PROG:wormhole
     4   LANG:C++
     5 */
     6 //官方解答
     7 #include <iostream>
     8 #include <fstream>
     9 using namespace std;
    10 #define MAX_N 12
    11 
    12 int N, X[MAX_N+1], Y[MAX_N+1];
    13 int partner[MAX_N+1];
    14 int next_on_right[MAX_N+1];
    15 
    16 bool cycle_exists(void)
    17 {
    18     for (int start=1; start<=N; start++) {
    19         // does there exist a cylce starting from start
    20         int pos = start;
    21         for (int count=0; count<N; count++)
    22         {
    23             pos = next_on_right[partner[pos]];
    24         }
    25         if (pos != 0) return true;
    26     }
    27     return false;
    28 }
    29 
    30 // count all solutions
    31 int solve(void)
    32 {
    33     // find first unpaired wormhole
    34     int i, total=0;
    35     for (i=1; i<=N; i++)
    36         if (partner[i] == 0) break;
    37 
    38     // everyone paired?
    39     if (i > N) {
    40         if (cycle_exists()) return 1;
    41         else return 0;
    42     }
    43 
    44     // try pairing i with all possible other wormholes j
    45     for (int j=i+1; j<=N; j++)
    46         if (partner[j] == 0) {
    47             // try pairing i & j, let recursion continue to
    48             // generate the rest of the solution
    49             partner[i] = j;
    50             partner[j] = i;
    51             total += solve();
    52             partner[i] = partner[j] = 0;
    53         }
    54     return total;
    55 }
    56 
    57 int main(void)
    58 {
    59     ifstream fin("wormhole.in");
    60     fin >> N;
    61     for (int i=1; i<=N; i++) fin >> X[i] >> Y[i];
    62     fin.close();
    63 
    64     for (int i=1; i<=N; i++) // set next_on_right[i]...
    65         for (int j=1; j<=N; j++)
    66             if (X[j] > X[i] && Y[i] == Y[j]) // j right of i...
    67                 if (next_on_right[i] == 0 ||
    68                     X[j]-X[i] < X[next_on_right[i]]-X[i])
    69                     next_on_right[i] = j;
    70     //找出紧邻的右边平行点
    71 
    72     ofstream fout("wormhole.out");
    73     fout << solve() << "
    ";
    74     //  cout<<solve()<<endl;
    75     fout.close();
    76     return 0;
    77 }
  • 相关阅读:
    Linux C语言编程基础
    《信息安全系统设计与实现》学习笔记二
    《信息安全系统设计与实现》选做一
    《信息安全系统设计与实现》学习笔记一
    商用密码调研
    20191212斯廷响 2020-2021-2 《Python程序设计》实验四报告
    20191212 2020-2021-2 《Python程序设计》实验三报告
    20191212 2020-2021-2 《Python程序设计》实验二报告
    20191212 2020-2021-2 《Python程序设计》实验一报告
    学习笔记2
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/4999769.html
Copyright © 2011-2022 走看看