標籤:
直接按照題意暴力就行
/*ID: 18906421LANG: C++PROG: wormhole*/#include<cstdio>#include<cstring>#include<vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 15;LL v[maxn];int next[maxn],connect[maxn];int n;int cnt = 0;int vis2[maxn];void debug(){ for(int i = 0; i < n; i++) printf("%d ",next[i]); puts("");}struct Point{ LL x,y; Point(LL x = 0,LL y = 0):x(x),y(y){}; friend bool operator < (Point a,Point b){ if(a.y != b.y) return a.y < b.y; else return a.x < b.x; }}p[maxn];bool solve(int pos){ //printf("%d -> ",pos); if(pos == -1) return true; if(vis2[next[pos]]) return false; if(next[pos] == -1) return true; vis2[next[pos]] = 1; return solve(connect[next[pos]]);}bool judge(){ for(int i = 0; i < n; i++){ memset(vis2,0,sizeof(vis2)); vis2[i] = 1; //printf("%d %d\n",i,connect[i]); bool ok = solve(connect[i]); //puts(""); if(!ok){ //printf("[%d]\n",i); return false; } } return true;}void dfs(int pos){ if(pos == n){ if(!judge()){ //for(int i = 0; i < n; i++) printf("[%d] %d ",i,connect[i]); puts(""); cnt ++; } return; } if(connect[pos] == -1) for(int i = 0; i < n; i++){ if(connect[i] == -1 && i != pos){ connect[pos] = i; connect[i] = pos; dfs(pos + 1); connect[i] = -1; connect[pos] = -1; } } else dfs(pos + 1);}int main(){ //freopen("wormhole.in","r",stdin); //freopen("wormhole.out","w",stdout); cin >> n; for(int i = 0; i < n; i++){ cin >> p[i].x >> p[i].y; next[i] = -1; connect[i] = -1; } sort(p,p + n); for(int i = 0; i < n - 1; i++) if(p[i].y == p[i + 1].y) next[i] = i + 1; //debug(); dfs(0); printf("%d\n",cnt); return 0;}
【USACO】Wormholes(暴力搜尋)