【USACO】Wormholes(暴力搜尋)

來源:互聯網
上載者:User

標籤:

直接按照題意暴力就行

/*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(暴力搜尋)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.