usaco1.3.4 Wormhole

来源:互联网 发布:网络歌曲排行榜2008 编辑:程序博客网 时间:2024/06/01 07:39

一. 原题

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):
40 01 01 10 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: wormholeLANG: C++Compiling...Compile: OKExecuting...   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 yoursubmission #3 for this problem. Congratulations!



AC代码:
/*ID:maxkibb3LANG: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;}


0 0
原创粉丝点击