USACO 1.3 Wormholes


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.

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,2黑洞是一對,從1黑洞進去會從2黑洞出來,從2黑洞進去會從1黑洞出來,然后他蠢得要死的奶牛不會躲,而且傻乎乎地往x正半軸的方向走,他的奶牛可能進入死循環,然后就gg了……然后這個炸出黑洞的人並不知道哪兩個黑洞聯通,我們要計算這些黑洞會在任意一點出現死循環的配對數

我們離散化一下,然后暴力搭配,然后在任意一個黑洞模擬走路就可以了,就是最后一個模擬沒寫好,掛了三次……

 1 /*
 2 PROB: wormhole
 3 LANG: C++
 4 ID: jiaqi si
 5 */
 6 #include <iostream>
 7 #include <string.h>
 8 #include <cstdlib>
 9 #include <cstdio>
10 #include <cmath>
11 #include <algorithm>
12 #include <cstring>
13 #include <vector>
14 #define ivory
15 #define mo  1000000007 
16 #define siji(i,x,y) for(int i=(x);i<=(y);i++)
17 #define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
18 #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
19 #define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
20 #define pii pair<int,int>
21 #define fi first
22 #define se second
23 #define mo 1000000007
24 using namespace std;
25 pii a[15];
26 int n,pline[15];
27 int paring[15],now;
28 bool use[15];
29 vector<int> v[15];
30 int ans;
31 bool check(int k,int prev) {
32     if(prev!=paring[k]) { return check(paring[k],k);}
33     //只要它過黑洞的時候不走回去就行,之前打了標記,然而有些時候黑洞可以走兩次
34     else {
35         int s=v[pline[k]].size();
36         xiaosiji(i,0,s) {
37             if(a[v[pline[k]][i]].fi==a[k].fi) {
38                 if(i==s-1) return true;
39                 if(v[pline[k]][i+1]==now) return false;
40                 return check(v[pline[k]][i+1],k);
41                 break;
42             }
43         }
44     }
45     return true;
46 }
47 void calculate() {
48     siji(i,1,n) {
49         now=i;
50         if(!check(i,0)) {++ans;return;}
51     }
52     
53 }
54 void dfs(int u,int t) {
55     if(t==n-2) {
56         siji(i,1,n) if(!use[i]) {paring[u]=i;paring[i]=u;} 
57         calculate();return;
58     }
59     use[u]=1;
60     siji(i,1,n) {
61         if(!use[i]) {
62             use[i]=1;paring[i]=u;paring[u]=i;
63             siji(j,1,n) {
64                 if(!use[j]) {dfs(j,t+2);break;}
65             }
66             use[i]=0;
67         }
68     }
69     use[u]=0;
70 }
71 int main()
72 {
73 #ifdef ivory
74     freopen("wormhole.in","r",stdin);
75     freopen("wormhole.out","w",stdout);
76 #else 
77     freopen("f1.in","r",stdin);
78 #endif
79     scanf("%d",&n);
80     siji(i,1,n) {
81         scanf("%d%d",&a[i].fi,&a[i].se);
82         swap(a[i].fi,a[i].se);
83     }
84     sort(a+1,a+n+1);
85     int it=a[1].fi,cnt=0;
86     siji(i,1,n) {
87         if(a[i].fi==it) {v[cnt].push_back(i);pline[i]=cnt;}
88         else {it=a[i].fi;++cnt;v[cnt].push_back(i);pline[i]=cnt;}
89         swap(a[i].fi,a[i].se);
90     }
91     dfs(1,0);
92     printf("%d\n",ans);
93     return 0;
94 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM