usaco1.3.4 Wormhole
来源:互联网 发布:网络歌曲排行榜2008 编辑:程序博客网 时间:2024/06/01 07:39
一. 原题
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.
二. 分析
三. 代码
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!
/*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;}
- usaco1.3.4 Wormhole
- USACO1.3.5 Wormholes (wormhole)
- USACO1.3.5 Wormholes (wormhole)
- USACO1.3 wormhole
- [USACO1.3]虫洞wormhole 暴力 (第一章完结)
- [USACO1.3] 虫洞 wormhole (暴搜)
- USACO1.3.4牛式
- USACO wormhole
- usaco wormhole
- [vijos1763] wormhole
- Wormhole大盘
- [vijos1763]Wormhole
- USACO1.3.4 Prime Cryptarithm(牛式)
- USACO1.3.4 Combination Lock (combo)
- USACO1.3.4 Combination Lock (combo)
- ZOJ 3613 Wormhole Transport
- Vijos P1763 Wormhole
- usaco 1.3.7wormhole
- uva816 Abbott’s Revenge
- 骚年,有空就写写博客吧
- 如何通scanf获取一个句子
- Oracle 11g RAC CRS-4535/ORA-15077
- (八)适配器模式详解
- usaco1.3.4 Wormhole
- 1926
- 从下往上的信息滚动特效
- 怎么使用golang的channel做广播
- 写自己的命令
- 【caffe配置】face_detection_alighment在windows10+caffe+vs2015下的配置
- (九)模板方法模式详解(包含与类加载器不得不说的故事)
- 构建应用程序窗口小部件(App Widget)
- c++如何计算带空格的字符串长度