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 (i.e., all the different ways that N wormholes could be paired such that Bessie can, in some way, get in a cycle). Note that a loop with a smaller number of wormholes might contribute a number of different sets of pairings to the total count as those wormholes that are not in the loop are paired in many different ways.
农夫约翰在周末进行高能物理实验的爱好已经适得其反,导致他的农场出现N个虫洞(2 <= N <= 12,N均匀),每个虫洞都位于他农场的2D地图上的一个独特点上( x,y坐标都是整数)。
根据他的计算,Farmer John知道他的虫洞会形成N / 2对。例如,如果虫洞A和B成对连接,那么进入虫洞A的任何物体将从同一方向移动的虫洞B出口,并且任何进入虫洞B的物体将类似地从同一方向上移动的虫洞A中退出。这可能会产生相当不愉快的后果。
例如,假设在(1,1)处有两个成对的虫洞A,在(3,1)处有B,并且Bessie从位置(2,1)开始向+ x方向移动。Bessie将进入虫洞B [at(3,1)],退出A [at(1,1)],然后再次进入B,依此类推,陷入无限循环!
| 。。。。 | A> B. Bessie将前往B +。。。。然后又到了B
农夫约翰知道他农场上每个虫洞的确切位置。他知道Bessie牛总是沿+ x方向行走,尽管他不记得Bessie目前的位置。
请帮助Farmer John计算虫洞明显配对的数量,这样如果Bessie从不幸的位置开始就可能陷入无限循环。FJ不知道哪个虫洞与任何其他虫洞配对,因此找到所有可能性(即,N个虫洞可以配对的所有不同方式,使得Bessie可以在某种程度上进入一个循环)。请注意,具有较少数量的虫洞的循环可能会为总计数提供许多不同的配对集,因为那些不在循环中的虫洞以许多不同的方式配对。
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.
代码如下:
/*
ID: zhaoxia13
LANG: C
TASK: wormhole
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int n, ans, next[MAX], pair[MAX];
struct node {
int x, y;
}a[MAX];
void build_edge() {
int dis[MAX];
int i, j, k;
memset(dis, -1, sizeof(dis));
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(a[i].y == a[j].y && a[j].x > a[i].x)
{
k = a[j].x - a[i].x;
if(dis[i] == -1 || dis[i] > k)
{
dis[i] = k;
next[i] = j;
}
}
}
}
}
int judge()
{
int i, j;
for(i = 0; i < n; i++)
{
int pos = i;
for(j = 0; j < n; j++)
{
pos = next[pair[pos]];
if(pos == -1) break;
}
if(pos != -1)
return 1;
}
return 0;
}
void make_pair()
{
int i, j;
for(i = 0; i < n; i++)
{
if(pair[i] == -1) break;
}
if(i == n)
{
if(judge()) ans++;
return;
}
for(j = i + 1; j < n; j++) {
if(pair[j] != -1) continue;
pair[i] = j;
pair[j] = i;
make_pair();
pair[j] = -1;
}
pair[i] = -1;
}
int main()
{
freopen("wormhole.in", "r", stdin);
freopen("wormhole.out", "w", stdout);
int i;
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
}
memset(next, -1 ,sizeof(next));
build_edge();
memset(pair, -1 ,sizeof(pair));
make_pair();
printf("%d\n", ans);
fclose(stdin);
fclose(stdout);
return 0;
}