洛谷 P1444 [USACO1.3]虫洞wormhole
https://www.luogu.org/problemnew/show/P1444
JDOJ 2386: USACO 2013 Dec Bronze 3.Wormholes
https://neooj.com:8082/oldoj/problem.php?id=2386
Description
Problem 3: Wormholes [Brian Dean, 2013]
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.
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 (0,0) and B at (1,0), and that Bessie the cow starts from
position (1/2,0) moving in the +x direction. Bessie will enter wormhole B,
exit from A, then enter B again, and so on, getting trapped in an infinite
cycle!
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.
Input
* 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.
Output
* 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 Input
4 0 0 1 0 1 1 0 1Sample Output
2HINT
INPUT DETAILS:
There are 4 wormholes, forming the corners of a square.
OUTPUT DETAILS:
If we number the wormholes 1..4, then by pairing 1 with 2 and 3 with 4,
Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or
between (0,1) and (1,1). Similarly, with the same starting points, Bessie
can get stuck in a cycle if the pairings are 1-3 and 2-4. 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.
#include<cstdio> #include<algorithm> using namespace std; int n,ans; int v[20]; struct node { int x,y; }w[20]; bool cmp(node a,node b) { if(a.y==b.y) return a.x<b.x; return a.y<b.y; } int find(int vis,int end,int begin,int way) { if(vis!=1 && end==begin && way==1) return 1; if(way==0) { if(w[end].y==w[end+1].y) return find(vis+1,end+1,begin,1); return 0; } if(way==1) return find(vis+1,v[end],begin,0); } int judge() { for(int i=1;i<=n;i++) if(find(1,i,i,1)) return 1; return 0; } void dfs(int x) { if(x==n+1) { if(judge()) ans++; return; } if(v[x]==0) { for(int i=x+1;i<=n;i++) if(v[i]==0) { v[i]=x;v[x]=i; dfs(x+1); v[i]=v[x]=0; } } if(v[x]!=0) dfs(x+1); return; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&w[i].x,&w[i].y); sort(w+1,w+n+1,cmp); dfs(1); printf("%d",ans); return 0; }