题目大意:

农场有N个洞(2<=N<=12,N为偶数),且组成N/2个连接对。每一个洞的给出平面坐标(x,y)。

假设A和B相连,则进入A会从B出来,进入B会从A出来。有仅仅会沿着x轴正方向前进的牛,非常可能就会陷入一个循环中。比如,A(1,1)。B(3,1),这头牛从(2,1)出发,首先会进入B,然后从A出来,由于牛仅仅沿着x方向前进,牛又进入了B,就这样无限循环下去了。

给定N个洞的坐标,牛能够从不论什么点出发,求全部可能出现 牛陷入循环的可能情况数(及N个洞的配对情况)

分析:

这个题目有个关键点,就是牛的走向。及牛仅仅能沿着x轴的正方向行走,要想形成无限循环,及每 当牛每次经过一个pair后,总能在其x正方向处存在下一个点。

所以题目的关键变为 寻找 平行的右邻接点

N的数目较小。对全部的情况进行列举就可以。

另外,无限循环的检測。发现一个 周期 就可以,或者。走过的次数大于 各个周期最大行走次数 后。这个点还有平行的右邻接点。由于一共就N个点。周期最大行走次数 肯定不会大于N (是否 不会大于N/2 还有待思考 )

/*
ID: abc18711
LANG: C
TASK: wormhole
*/
#include <stdio.h>
#include <string.h> #define MAXN 12+1 int X[MAXN];
int Y[MAXN]; int pair[MAXN];
int right_hole[MAXN]; int N; int exist_cycle()
{
int start_hole;
int flag;
int i; for (start_hole=1; start_hole<=N; start_hole++)
{
for (i=1,flag=start_hole; i<=N; i++)
{
flag = right_hole[ pair[flag] ];
if(flag == 0) break;
}
if (flag > 0) return 1;
} return 0; } // the pair combination int solutions()
{
int i;
int j;
int total = 0; //find first unpaired hole
for (i=1; i<=N; i++)
if(pair[i] == 0) break; //no unpaired hole,test the cycle
if (i > N){
return exist_cycle();
} //pairing i and j
for (j=i+1; j<=N; j++)
if (pair[j] == 0)
{
pair[i] = j;
pair[j] = i;
total += solutions();
pair[i] = pair[j] = 0;
} return total;
} int main()
{
int i;
int j; FILE *fin = fopen("wormhole.in", "r");
FILE *fout = fopen("wormhole.out", "w"); // get the input
fscanf(fin, "%d", &N); for (i=1; i<=N; i++)
fscanf(fin, "%d %d", &X[i], &Y[i]); memset(pair, 0, MAXN*sizeof(int));
memset(right_hole, 0, MAXN*sizeof(int)); // find the right hole
for( i=1; i<=N; i++)
for (j=1; j<=N; j++)
if (Y[i] == Y[j] && X[j] > X[i])
if (right_hole[i] == 0 || (X[j]-X[i]) < (X[ right_hole[i] ]-X[i]))
right_hole[i] = j; fprintf(fout, "%d\n", solutions()); return 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.

UASCO Wormholes 解析 and C 语言实现的更多相关文章

  1. 二叉查找树(一)之 图文解析 和 C语言的实现

    概要 本章先对二叉树的相关理论知识进行介绍,然后给出C语言的详细实现.关于二叉树的学习,需要说明的是:它并不难,不仅不难,而且它非常简单.初次接触树的时候,我也觉得它似乎很难:而之所产生这种感觉主要是 ...

  2. AVL树(一)之 图文解析 和 C语言的实现

    概要 本章介绍AVL树.和前面介绍"二叉查找树"的流程一样,本章先对AVL树的理论知识进行简单介绍,然后给出C语言的实现.本篇实现的二叉查找树是C语言版的,后面章节再分别给出C++ ...

  3. 伸展树(一)之 图文解析 和 C语言的实现

    概要 本章介绍伸展树.它和"二叉查找树"和"AVL树"一样,都是特殊的二叉树.在了解了"二叉查找树"和"AVL树"之后, ...

  4. 二叉堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...

  5. 左倾堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍左倾堆,它和二叉堆一样,都是堆结构中的一员.和以往一样,本文会先对左倾堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理 ...

  6. 二项堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍二项堆,它和之前所讲的堆(二叉堆.左倾堆.斜堆)一样,也是用于实现优先队列的.和以往一样,本文会先对二项堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...

  7. 斐波那契堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍斐波那契堆.和以往一样,本文会先对斐波那契堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了 ...

  8. URL的解析,C语言实现

    源: URL的解析,C语言实现 c语言实现urlencode和decode

  9. .NET面试题解析(11)-SQL语言基础及数据库基本原理

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 本文内容涉及到基本SQL语法,数据的基本存储原理,数据库一些概念.数据优化等.抱砖引玉,权当一个综合复习! ...

随机推荐

  1. Activiti 流程启动及节点流转源代码分析

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文主要是以activiti-study中的xiaomage.xml流程图为例进行跟踪分析 详细的 ...

  2. T-SQL 之 语法元素

    一.标识符 在T-SQL语言中,对SQLServer数据库及其数据对象(比如表.索引.视图.存储过程.触发器等)需要以名称来进行命名并加以区分,这些名称就称为标识符. 通常情况下,SQLServer数 ...

  3. Java生成各种条形码

    import java.awt.image.BufferedImage; import java.io.FileOutputStream; import org.jbarcode.JBarcode; ...

  4. 33、深入理解Java的接口和抽象类

    深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...

  5. 零基础小白怎么用Python做表格?

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件.本文用Python3!在给大家分享之前呢,小编推荐一下一个挺不错的交流宝地,里面都是一群热爱并在学 ...

  6. iOS开发:icon和启动图尺寸(转)

    转至:http://www.cnblogs.com/shaoting/p/4941634.html 歪果仁的总结: Asset iPhone 6s Plus and iPhone 6 Plus (@3 ...

  7. 计算机必知必会:进程process与线程thread 进程定义为一个正在运行的程序的实例

    http://www.nowamagic.net/librarys/veda/detail/1741进程和线程这对概念的理解也是很难的,至今网络上可查的资料对其的理解出入都挺大,在不同的操作系统中,如 ...

  8. spring3.1之前的HandlerMapping ,HandlerAdapter以及spring3.1写法

    <!--Spring3.1之前的注解 HandlerMapping --><!-- <bean class="org.springframework.web.serv ...

  9. 深入理解Android的密度独立性

    前言 安卓是一个只对硬件设备限制有很少限制的移动操作系统.生产商们几乎可以创造任何形状的.尺寸的和密度的屏幕的设备.设备可以有物理键盘和按钮或者只有虚 拟键盘和按钮.由于它的设备客制化的自由性给软件开 ...

  10. 常用音频软件:Wavesufer

    作者:桂. 时间:2017-06-02  10:23:39 链接:http://www.cnblogs.com/xingshansi/p/6932408.html 前言 只列举两个自己用过的(wave ...