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

时间: 2024-08-30 17:00:39

usaco-1.3-wormhole的相关文章

Usaco Training [1.3] wormhole

传送门 解题要素:代码能力 解题步骤:理解题意 - >搜索枚举所有可能的配对情况 - >判断冲突并求解 - >调试 一. 理解题意 这里讲几个不容易理解的点: 1. +x方向 即向右走 2. 一旦来到虫洞,就必须掉入 二. 搜索枚举所有可能的配对情况 考虑引入match数组,对于当前的节点来说,枚举后面的点是否已配对过即可 1 int match[N]; 2 inline void dfs(int x) { //当前节点 3 if (x == n + 1) { 4 rep(i, 1, n

Wormholes USACO

先给个任意门: http://train.usaco.org/usacoprob2?a=Hjk3nSx2aDB&S=wormhole 又是一道好题直接解释吧~ x,y -> positionr[u] -> 第u个点右边第一个hole的位置partner[u] -> 与u相连的点 Then 怎么判断她就陷入循环了呢?也就是以下代码中n-1次判断走过虫洞并判断虫洞. pos表示从第pos个虫洞进入,进入后,我们自然就到达了 partener [pos]那下一个位置就是 r[ part

[题解]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 coordin

USACO Section1.3 Wormholes 解题报告

wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 一个人在二维坐标系上走,方向永远是+x.此坐标系中有N个虫洞(N是偶数). 虫洞这东西,一旦两个配成一对,便可以形成“传送门

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f

USACO Chapter 1 Section 1.1

USACO的题解和翻译已经很多了... 我只是把自己刷的代码保存一下. 1.PROB Your Ride Is Here 1 /* 2 ID:xiekeyi1 3 PROG:ride 4 LANG:C++ 5 */ 6 7 #include<bits/stdc++.h> 8 using namespace std ; 9 10 int main() 11 { 12 freopen("ride.in","r",stdin); 13 freopen(&quo

USACO Your Ride Is Here

[USACO]Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do,

usaco月赛,2017.1总结

T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现在给出奶牛个数,最多用时,每头奶牛的跳舞时间.求舞台最小为多大. 思路:本来写了个程序以为这道题很简单,刚开始排一下序然后就行了,结果交了以后发现只过了五组,然后才发现这道题不能改变顺序(所以说为什么我改变顺序了还是能过五组,usaco的数据也好水......),所以说我想到了堆,然后就用堆写了一下