Section1.3-5 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 (i.e., all the different ways that N wormholes could be paired such that Bessie can, in some way, get in a cycle). Note that a loop with a smaller number of wormholes might contribute a number of different sets of pairings to the total count as those wormholes that are not in the loop are paired in many different ways.
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.
比较综合的一道题。题目大致分为三个部分:找匹配,建图和找解
找匹配可以大致理解为找1~n的全排列(不过要考虑去掉一些可能重复的情况),也就是把1~n按不同顺序推入vector中,其中第i个(i%2!=0)和第i+1个为一对虫洞
之后建图。就是保证一对虫洞可以互相穿越就好了
找解的时候要枚举牛的初始位置(由此可知最开始应该先把所有虫洞排个序)。然后我用io记录当前是入洞(io=0)还是出洞(io=1)。
入洞:如果当前洞已经进入过一次,则可以肯定牛已经开始循环,退出且ans++;否则穿越到与其对应的虫洞
出洞:此时需要寻找下一个可以入洞的虫洞,即保证两个虫洞的纵坐标相同且下一个虫洞在当前虫洞的右边(这里也可以看出排序的必要性),找到后入洞。
1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 using namespace std; 9 int n; 10 int ans; 11 bool b[20]; 12 struct data 13 { 14 int x,y; 15 bool operator < (const data &a) const 16 { 17 if(x<a.x) return true; 18 else if(x==a.x && y<a.y) return true; 19 else return false; 20 } 21 }a[20]; 22 vector <int> p; 23 int map[20]; 24 bool used[20]; 25 bool dfs(int m,bool io) //找解 26 { 27 if(!io && used[m]) return true; 28 if(!io) {used[m]=true;return dfs(map[m],1);} //dfs函数不是void型!!!所以要记得加return!!! 29 else //开始一直没加这个else,没想到竟然会影响结果 30 { 31 int ok=false; 32 for(int i=m+1;i<=n;i++) 33 { 34 if(a[i].y==a[m].y) {ok=true;return dfs(i,0);} 35 if(ok) break; 36 } 37 if(!ok) return false; 38 } 39 } 40 41 void check() //建图 42 { 43 memset(map,0,sizeof(map)); 44 for(int i=0;i<n;i+=2) //这里一定要从0开始,因为vector里默认从0开始 45 { 46 //cout<<p[i]<<" "; 47 map[p[i]]=p[i+1]; 48 map[p[i+1]]=p[i]; 49 } 50 /*for(int i=1;i<=n;i++) cout<<map[i]<<" "; 51 cout<<endl;*/ 52 } 53 54 void find(int pre) //找匹配 55 { 56 p.push_back(pre); 57 b[pre]=true; 58 for(int i=2;i<=n;i++) 59 { 60 if(!b[i]) 61 { 62 b[i]=true; 63 p.push_back(i); 64 if(p.size()==n) 65 { 66 check(); 67 for(int j=1;j<=n;j++) 68 { 69 memset(used,0,sizeof(used)); 70 if(dfs(j,0)) {ans++;break;} 71 } 72 } 73 else 74 { 75 for(int j=1;j<=n;j++) 76 if(!b[j]) 77 { 78 find(j); 79 break; //防止重复配对 80 } 81 } 82 p.pop_back(); 83 b[i]=false; 84 } 85 } 86 p.pop_back(); 87 b[pre]=false; 88 } 89 90 int main() 91 { 92 freopen("wormhole.in","r",stdin); 93 freopen("wormhole.out","w",stdout); 94 scanf("%d",&n); 95 for(int i=1;i<=n;i++) 96 scanf("%d%d",&a[i].x,&a[i].y); 97 sort(a+1,a+n+1); 98 find(1); 99 printf("%d\n",ans); 100 //system("pause"); 101 return 0; 102 }

浙公网安备 33010602011771号