• usaco-1.3-wormhole


    这个属于DFS枚举:

    /*
    ID: qq104801
    LANG: C++
    TASK: wormhole
    QQ:104804687
    */
    
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    #define loop(i,n) for(int i=0;i<(n);i++)
    #define loop2(i,n) for(int i=1;i<=(n);i++)
    const int maxn=13;
    const int inf=1<<30;
    
    int n,x[maxn],y[maxn];
    int partner[maxn],next_on_right[maxn];
    
    bool cycle_exists(void)
    {
      /*
      for(int i=1;i<=n;i++)
        cout<<i<<":"<<partner[i]<<" ";
      cout<<endl;
      */
    
      for(int start=1;start<=n;start++)
      {
        int pos=start;
        for(int count=0;count<n;count++)
          pos=next_on_right[partner[pos]];
        if(pos!=0) return true;
      }
      return false;
    }
    
    int solve(void)
    {
      int i,total=0;
      for(i=1;i<=n;i++)
        if(partner[i]==0)break;
    
      if(i>n)
      {
        if(cycle_exists())
          return 1;
        else
          return 0;
      }
    
      for(int j=i+1;j<=n;j++)
        if(partner[j]==0)
        {
          partner[i]=j;
          partner[j]=i;      
          total+=solve();
          partner[i]=partner[j]=0;
        }
      return total;
    }
    
    void test()
    {   
      freopen("wormhole.in","r",stdin);  
      freopen("wormhole.out","w",stdout); 
      cin>>n;
      loop2(i,n)
      {
        cin>>x[i]>>y[i];
        //cout<<x[i]<<":"<<y[i]<<endl;
      }
    
      loop2(i,n)
        loop2(j,n)
          if(x[j]>x[i]&&y[i]==y[j])
            if(next_on_right[i]==0 || x[j]-x[i]<x[next_on_right[i]]-x[i])
              next_on_right[i]=j;
    
      cout<<solve()<<endl;
    }
    
    int main () 
    {        
        test();        
        return 0;
    }

    test data:

    USACO Training
    Grader Results     
    4 users online
    CHN/2 IND/1 KAZ/1
    
    USER: cn tom [qq104801]
    TASK: wormhole
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.008 secs, 3380 KB]
       Test 2: TEST OK [0.008 secs, 3380 KB]
       Test 3: TEST OK [0.005 secs, 3380 KB]
       Test 4: TEST OK [0.011 secs, 3380 KB]
       Test 5: TEST OK [0.008 secs, 3380 KB]
       Test 6: TEST OK [0.011 secs, 3380 KB]
       Test 7: TEST OK [0.011 secs, 3380 KB]
       Test 8: TEST OK [0.014 secs, 3380 KB]
       Test 9: TEST OK [0.016 secs, 3380 KB]
       Test 10: TEST OK [0.019 secs, 3380 KB]
    
    All tests OK.
    
    Your program ('wormhole') produced all correct answers! This is your submission #2 for this problem. Congratulations!
    
    Here are the test data inputs:
    
    ------- test 1 [length 18 bytes] ----
    4
    0 0
    1 0
    1 1
    0 1
    ------- test 2 [length 23 bytes] ----
    4
    21 7
    11 23
    11 7
    5 10
    ------- test 3 [length 37 bytes] ----
    6
    1 15
    20 15
    17 11
    22 21
    25 11
    20 17
    ------- test 4 [length 117 bytes] ----
    6
    879221060 76350727
    161945371 76350727
    380619073 76350727
    896289713 76350727
    852891025 852349940
    519959866 76350727
    ------- test 5 [length 159 bytes] ----
    8
    491007315 324539677
    99582962 578796531
    491007315 768974420
    373902710 578796531
    99582962 332416760
    946724340 321414549
    976998120 638226676
    49126574 437845706
    ------- test 6 [length 198 bytes] ----
    10
    987878530 332490544
    545074228 332490544
    571194544 278963943
    32922985 229703843
    571194544 851333603
    90862786 28227282
    219975775 267376202
    219975775 332490544
    90862786 62367085
    872930617 951881113
    ------- test 7 [length 243 bytes] ----
    12
    636437309 704270751
    695056713 700147825
    636437309 356396548
    921201220 589666013
    430411607 671693685
    324259336 671693685
    723442153 589666013
    528904109 419799944
    921201220 356396548
    723442153 856537355
    691516566 726853849
    941903572 634527403
    ------- test 8 [length 53 bytes] ----
    12
    0 0
    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0
    10 1
    10 0
    ------- test 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
    ------- test 10 [length 236 bytes] ----
    12
    5138254 91583927
    607865472 167507876
    51248250 8250417
    675467597 611809280
    157130071 946061365
    138261433 967068106
    769112165 966993974
    675467597 144175980
    769112165 475105594
    51248250 144175980
    947874168 111530133
    164921238 967068106
    View Code
    /***********************************************

    看书看原版,原汁原味。

    不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。

    没有原书,也要网上找PDF来看。

    网上的原版资料多了去了,下载东西也到原始下载点去看看。

    你会知其所以然,呵呵。

    ***********************************************/

  • 相关阅读:
    函数指针Demo
    设计模式笔记
    Simple Factory 模式
    百度API操作实例
    如何发邮件不被认定为垃圾邮件的技巧
    DateTimePicker中自定义时间或日期显示格式
    取得指定月份的天数
    DataGridView中的内容太长时换行显示
    Numericupdown控件value值的非空判断
    C#中用SQL语句CreatTable
  • 原文地址:https://www.cnblogs.com/dpblue/p/4040571.html
Copyright © 2020-2023  润新知