usaco1.3.4 Wormhole

一. 原题

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. 

二. 分析

这题关键在make_worm_pair()和judge()这两个函数上。第一个函数对于给定的n个数,完成两两配对,关键要避免重复,如果把两个数叫做一个pair,其实只要注意选取pair的第一个数时保证是当前可选的数中最小的就行了。第二个函数判断一个有向图里有没有环。这题里有一个坑爹的“虫洞”概念,写的时候我还纠结了一会:在到达一个点后,是优先进入虫洞还是可以选择进入虫洞或向右走。其实题意讲的很清楚了,是优先进入虫洞。因此这个题给定起始点之后,能走的路径其实是唯一的。


三. 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: wormhole
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4192 KB]
   Test 2: TEST OK [0.000 secs, 4192 KB]
   Test 3: TEST OK [0.000 secs, 4192 KB]
   Test 4: TEST OK [0.000 secs, 4192 KB]
   Test 5: TEST OK [0.000 secs, 4192 KB]
   Test 6: TEST OK [0.000 secs, 4192 KB]
   Test 7: TEST OK [0.000 secs, 4192 KB]
   Test 8: TEST OK [0.000 secs, 4192 KB]
   Test 9: TEST OK [0.000 secs, 4192 KB]
   Test 10: TEST OK [0.000 secs, 4192 KB]

All tests OK.

Your program ('wormhole') produced all correct answers! This is your submission #3 for this problem. Congratulations!



AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:wormhole
*/

#include<cstdio>
#include<cstring>
using namespace std;

const int MAX = 15;
int n, ans, next[MAX], worm_pair[MAX];
struct node {
	int x, y;
}a[MAX];

void build_edge() {
	int dis[MAX];
	memset(dis, -1, sizeof(dis));
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			if(a[i].y == a[j].y && a[j].x > a[i].x) {
				int _dis = a[j].x - a[i].x;
				if(dis[i] == -1 || dis[i] > _dis) {
					dis[i] = _dis;
					next[i] = j;
				}
			}
		}
	}
}

bool judge() {
	for(int i = 0; i < n; i++) {
		int pos = i;
		for(int j = 0; j < n; j++) {
			pos = next[worm_pair[pos]];
			if(pos == -1) break;
		}
		if(pos != -1) return true;
	}
	return false;
}

void make_worm_pair() {
	int i;
	for(i = 0; i < n; i++) {
		if(worm_pair[i] == -1) break;
	}
	if(i == n) {
		if(judge()) ans++;
		return;
	}		
	for(int j = i + 1; j < n; j++) {			
		if(worm_pair[j] != -1) continue;
		worm_pair[i] = j;
		worm_pair[j] = i;
		make_worm_pair();
		worm_pair[j] = -1;
	}
	worm_pair[i] = -1;
}

int main() {
	freopen("wormhole.in", "r", stdin);
	freopen("wormhole.out", "w", stdout);
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d%d", &a[i].x, &a[i].y);
	}
	memset(next, -1 ,sizeof(next));
	build_edge();
	memset(worm_pair, -1 ,sizeof(worm_pair));
	make_worm_pair();
	printf("%d\n", ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值