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!

  1. | . . . .
  2. | A > B . Bessie will travel to B then
  3. + . . . . 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):

  1. 4
  2. 0 0
  3. 1 0
  4. 1 1
  5. 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):

  1. 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).

  1. | . . . .
  2. 4 3 . . . Bessie will travel to B then
  3. 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.


Submission file Name:  USACO Gateway |   Comment or Question

(转自[USACO])


  讲一下题目大意。Farmer John(USACO的标志人物啊)特别喜欢做实验,使得农场上出现了N(N <= 12)个虫洞,现在将这N个虫洞两两配对,配对了的虫洞从其中一个进入就会从对应的一个虫洞出来。Bessie在田园中总是向x轴的正方向前进。John想知道,有多少种虫洞的配对方案可以让Bessie在田园中某一个地方出发,会陷入死循环。

  很明显的搜索(数据范围小),因为(1,2)(3,4)和(3,4)(1,2)是同一种匹配,所以每一次搜索的时候就找到编号最小的一个虫洞和其他的虫洞匹配。

  接下来就是判断是否会陷入死循环。还算比较好想。首先给每个虫洞预处理出一个right,表示从它出来,往右走遇到的第一个虫洞的编号,如果不存在,就记个0吧。然后用个数组记录一下类似于Tarjan的时间戳的一个东西,每次循环访问的时候记一个编号(当然设成一样的),如果下一虫洞没有访问,那么就把编号设成当前的编号,否则判断它是否和现在的编号相等,如果相等,就说明会陷入死循环。

Code

  1. /*
  2. ID:
  3. PROG: wormhole
  4. LANG: C++11
  5. */
  6. /**
  7. * USACO
  8. * Accpeted
  9. * Time:0ms
  10. * Memory:4184k
  11. */
  12. #include<iostream>
  13. #include<cstdio>
  14. #include<cctype>
  15. #include<cstring>
  16. #include<cstdlib>
  17. #include<fstream>
  18. #include<sstream>
  19. #include<algorithm>
  20. #include<map>
  21. #include<set>
  22. #include<queue>
  23. #include<vector>
  24. #include<stack>
  25. using namespace std;
  26. typedef bool boolean;
  27. #define INF 0xfffffff
  28. #define smin(a, b) a = min(a, b)
  29. #define smax(a, b) a = max(a, b)
  30. template<typename T>
  31. inline void readInteger(T& u){
  32. char x;
  33. int aFlag = ;
  34. while(!isdigit((x = getchar())) && x != '-');
  35. if(x == '-'){
  36. x = getchar();
  37. aFlag = -;
  38. }
  39. for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
  40. ungetc(x, stdin);
  41. u *= aFlag;
  42. }
  43.  
  44. typedef class Point {
  45. public:
  46. int x;
  47. int y;
  48. int id;
  49. boolean operator < (Point another) const {
  50. if(this->y != another.y) return this->y < another.y;
  51. return this->x < another.x;
  52. }
  53. }Point;
  54.  
  55. int n;
  56. int* onright;
  57. Point* ps;
  58.  
  59. inline void init(){
  60. readInteger(n);
  61. ps = new Point[(const int)(n + )];
  62. onright = new int[(const int)(n + )];
  63. for(int i = ; i <= n; i++){
  64. readInteger(ps[i].x);
  65. readInteger(ps[i].y);
  66. ps[i].id = i;
  67. onright[i] = ;
  68. }
  69. }
  70.  
  71. int* matches;
  72. boolean* seced;
  73. int res;
  74.  
  75. boolean check(){
  76. int vis[(const int)(n + )];
  77. memset(vis, , sizeof(vis));
  78. vis[] = -;
  79. for(int i = ; i <= n; i++){
  80. if(vis[i] != ) continue;
  81. int p = i;
  82. while(vis[p] == ){
  83. vis[p] = i;
  84. p = onright[p];
  85. if(seced[p]) p = matches[p];
  86. }
  87. if(vis[p] == i) return true;
  88. }
  89. return false;
  90. }
  91.  
  92. void search(int choosed){
  93. if(choosed + > n){
  94. if(check()) res++;
  95. return;
  96. }
  97. int sta;
  98. for(int i = ; i <= n; i++)
  99. if(!seced[i]){
  100. sta = i;
  101. break;
  102. }
  103. seced[sta] = true;
  104. for(int i = sta + ; i <= n; i++){
  105. if(!seced[i]){
  106. matches[sta] = i;
  107. matches[i] = sta;
  108. seced[i] = true;
  109. search(choosed + );
  110. seced[i] = false;
  111. }
  112. }
  113. seced[sta] = false;
  114. }
  115.  
  116. inline void solve(){
  117. matches = new int[(const int)(n + )];
  118. seced = new boolean[(const int)(n + )];
  119. memset(seced, false, sizeof(boolean) * (n + ));
  120. sort(ps + , ps + n + );
  121. for(int i = ; i < n; i++){
  122. if(ps[i + ].y == ps[i].y){
  123. onright[ps[i].id] = ps[i + ].id;
  124. }
  125. }
  126. search();
  127. printf("%d\n", res);
  128. }
  129.  
  130. int main(){
  131. freopen("wormhole.in", "r", stdin);
  132. freopen("wormhole.out", "w", stdout);
  133. init();
  134. solve();
  135. return ;
  136. }

USACO 1.3 Wormholes - 搜索的更多相关文章

  1. [题解]USACO 1.3 Wormholes

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  2. USACO 1.3 Wormholes

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  3. USACO Section1.3 Wormholes 解题报告

    wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...

  4. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  5. P1466 集合 Subset Sums 搜索+递推+背包三种做法

    题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...

  6. 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」

    USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...

  7. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)

    这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...

  8. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ

    emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...

  9. POJ 1176 Party Lamps&& USACO 2.2 派对灯(搜索)

    题目地址 http://poj.org/problem?id=1176 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都 ...

随机推荐

  1. javascript 字符串去空格

    1.正则去空格 a.去掉字符串中所有空格 " hello world ".replace(/\s+/g,"");//helloworld b.去掉字符串左边空格 ...

  2. Net Promoter Score

    https://baike.baidu.com/item/净推荐值/3783368?fr=aladdin NPS(Net Promoter Score),净推荐值,又称净促进者得分,亦可称口碑,是一种 ...

  3. _cs, _ci, or _bin,

    High Performance MySQL, Third Edition by Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko   http:/ ...

  4. kerberos (https://en.wikipedia.org/wiki/Kerberos_(protocol))

    Protocol[edit] Description[edit] The client authenticates itself to the Authentication Server (AS) w ...

  5. 2018/04/04 每日一个Linux命令 之 ps

    ps 用于查看系统内的进程状态. 这个命令比较重要,也比较长,会通过实践出常用的命令 -- 当我们敲下一个 ps 之后会发生什么? ubuntu@hong:~/nginx/sites-enabled$ ...

  6. B. Berland National Library---cf567B(set|模拟)

    题目链接:http://codeforces.com/problemset/problem/567/B  题意:题目大意: 一个计数器, +号代表一个人进入图书馆, -号代表一个人出去图书馆. 给一个 ...

  7. CentOS安装Jdk并配置环境变量

    环境 CentOS7.2 (安装镜像CentOS-7-x86_64-DVD-1611) 目标 在CentOS7.2上安装jdk1.8(tar.gz安装包),并配置环境变量 jdk安装在/home/so ...

  8. 解决VMware虚拟机的CentOS无法上网

    1)点击 VM->Settings Hardware选项卡下面 2)点击Network Adapter 设置如下图所示,首先我们在虚拟机中将网络配置设置成NAT 在服务中开启: VMware D ...

  9. 测试Celery 在Windows中搭建和使用的版本

    官网:http://docs.celeryproject.org/en/latest/faq.html#does-celery-support-windows 描述如下:表示Celery 4.0版本以 ...

  10. [LeetCode] 183. Customers Who Never Order_Easy tag: SQL

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...