标签:
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.
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. |
4 0 0 1 0 1 1 0 1
There are 4 wormholes, forming the corners of a square.
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. |
2
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.
题解: 简单的深搜问题, 找两两配对且满足题意的组合数。
可是我开始也一直不会做,看了题解 才做出来,结果代码与官方代码相似度奇高。
ps:本人大三狗一枚,正在持续更新博客,文章里有任何问题,希望各位网友可以指出。若有疑问也可在评论区留言,我会尽快回复。希望能与各位网友互相学习,谢谢
/* ID: cxq_xia1 PROG: wormhole LANG: C++ */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=15; int X[maxn],Y[maxn]; int N; int nextRight[maxn],partner[maxn]; bool IsCycle() //判断是否形成循环 { for(int start=1;start<=N;start++) { int pos=start; for(int cnt=0;cnt<N;cnt++) { pos=nextRight[partner[pos]]; } if(pos!=0) return true; } return false; } int solve() //DFS计算一共有多少种配对方法 { int i,ans=0; for(i=1;i<=N;i++) //找到第一个没有没有被匹配的虫洞i,把他拿出来 { if(partner[i]==0) break; } if(i>N) //当所有的虫洞都匹配了 { if(IsCycle()) //如果可以形成循环返回1,否则返回0 return 1; else return 0; } for(int j=i+1;j<=N;j++) //把虫洞i拿去和其他没匹配的虫洞一一匹配 { if(partner[j]==0) { partner[i]=j; partner[j]=i; ans+=solve(); partner[i]=partner[j]=0; //递归退层的撤销操作 } } return ans; } int main() { freopen("wormhole.in","r",stdin); freopen("wormhole.out","w",stdout); memset(partner,0,sizeof(partner)); memset(nextRight,0,sizeof(nextRight)); cin >> N; for(int i=1;i<=N;i++) cin >> X[i] >> Y[i]; for(int i=1;i<=N;i++) //找到每个点与他Y坐标相同且最近的点 { for(int j=1;j<=N;j++) { if(X[j]>X[i]&&Y[i]==Y[j]) { if(nextRight[i]==0||X[j]<X[nextRight[i]]) { nextRight[i]=j; } } } } cout << solve() <<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/WillsCheng/p/4790423.html