USACO Wormholes 解题日志
来源:互联网 发布:零基础学java要多久 编辑:程序博客网 时间:2024/06/05 22:53
表示PPCA做题还是挺快乐的。虽然还不知道那个大作业到底是什么题。
先贴上这道题的题目:
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.
一开始还以为要用什么数据结构,把这道题想复杂了。后来Google了一下(QAQ好难过),发现网上有很多都是官方爸爸的做法。
我们采用直接开数组记录右邻居的方法:对于一个洞A,用nextRight[A]记下和它右边的和它纵坐标相等的洞中最靠近它的洞的下标。然后如果要判断有没有环,依次走下去就好了。具体实现在函数exiCycle()中,就不用多说。
然后注意的一点是,我们的pair[i],pair[j]中的i,j不是横坐标值,而是横坐标对应的输入时的下标值。一开始做的时候不小心搞混了,查了好久QAQ。
下面是代码:
#include<fstream>#define maxn 15using namespace std;ifstream fin("wormhole.in");ofstream fout("wormhole.out");int n, x[maxn], y[maxn];//int total;int nextRight[maxn];int pairs[maxn];bool exiCycle(){ for (int i = 1; i <= n; ++i){ int pos = i; for (int j = 1; j <= n; ++j) pos = nextRight[pairs[pos]]; if (pos != 0) return 1; } return 0;}int solve(){ int i = 1; int total = 0; for (i = 1; i <= n; ++i) if (pairs[i] == 0) break; if (i > n){ if (exiCycle()) return 1; else return 0; } for (int j = i + 1; j <= n; ++j){ if (pairs[j] == 0){ pairs[i] = j; pairs[j] = i; total += solve(); pairs[i] = pairs[j] = 0; } } return total;}int main(){ fin >> n; for (int i = 1; i <= n; ++i) fin >> x[i] >> y[i]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j){ if (y[i] == y[j] && x[i] < x[j]){ if (nextRight[i] == 0 || x[nextRight[i]] - x[i] > x[j] - x[i]) nextRight[i] = j; } } fout << solve() << endl; fin.close(); fout.close(); return 0;}
- USACO Wormholes 解题日志
- USACO Wormholes 解题报告
- USACO Transformations 解题日志
- USACO Wormholes
- USACO Broken Necklace 解题日志
- USACO Milking Cows 解题日志
- USACO Palindromic Squares 解题日志
- USACO Dual Palindromes 解题日志
- USACO Mixing Milk 解题日志
- USACO Barn Repair 解题日志
- USACO Combination Lock 解题日志
- USACO Arithmetic Progressions 解题日志
- USACO Your Ride Is Here 解题日志
- USACO Greedy Gift Givers 解题日志
- USACO Friday the Thirteenth 解题日志
- USACO Name That Number 解题日志
- USACO Ski Course Design 解题日志
- USACO Section 1.3 Wormholes
- 如何将Android Studio项目提交(更新)到github作为远程仓库
- maven deploy 之父子项目依赖
- C++ 对象的内存布局(重复继承)
- 使用delphi 开发多层应用(二)为什么要使用多层开发?
- sbt从入门到半熟
- USACO Wormholes 解题日志
- 符号表(Symbol Table)
- DoG(Difference of Gaussian)
- CSS选择器
- 堆和栈的区别
- 自定义过渡动画
- 有关Logstash的笔记
- iOS 加载网络视频之(AVFoundation.framewor)的MPMoviePlayerController
- win10+cocos2d-x3.0rc开发指南(2)--完善版