码迷,mamicode.com
首页 > 其他好文 > 详细

【USACO】Wormholes(暴力搜索)

时间:2015-04-13 22:56:49      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

直接按照题意暴力就行

/*
ID: 18906421
LANG: 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(暴力搜索)

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/45032883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!