/*
ID:
PROG: wormhole
LANG: C++11
*/
#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 15
int partner[MAXN];
int nextOnRight[MAXN];
int n;
struct HOLE{
int x, y;
}hole[MAXN];
int circle(){
for (int i = 0; i < n; i ++){
int pos = i;
for (int j = 0; j < n && pos != -1; j ++){
pos = nextOnRight[partner[pos]];
}
if (pos != -1)
return 1;
}
return 0;
}
int solve()
{
int i;
for (i = 0; i < n; i ++)
if (partner[i] == -1)
break;
if (i == n){
return circle();
}
int j;
int total = 0;
for (j = i + 1; j < n; j ++){
if (partner[j] == -1) {
partner[i] = j;
partner[j] = i;
total += solve();
partner[i] = partner[j] = -1;
}
}
return total;
}
int main() {
freopen("wormhole.in", "r", stdin);
freopen("wormhole.out", "w", stdout);
cin >> n;
for (int i = 0; i < n; i ++){
cin >> hole[i].x >> hole[i].y;
}
memset(nextOnRight, -1, sizeof(nextOnRight));
memset(partner, -1, sizeof(partner));
for (int i = 0; i < n; i ++){
for (int j = 0; j < n; j ++){
if (hole[j].y == hole[i].y && hole[j].x > hole[i].x){
if (nextOnRight[i] == -1 || hole[nextOnRight[i]].x > hole[j].x){
nextOnRight[i] = j;
}
}
}
}
int ans = solve();
cout << ans << endl;
return 0;
}
【USACO】Wormhole
最新推荐文章于 2022-07-31 22:29:42 发布