USACO Section 1.3 wormhole (暴力+模拟)

注意几个地方。

1,暴力的时候要注意别算重复了。匹配是双向的。

2,判断回路不能使用拓扑排序或弗洛伊德,因为它的行动是有规则的:传送以后只能往前走不能再相同位置传回去,基于这个条件只能枚举每个位置模拟判环。


#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=15;
struct Node
{
    int x,y,num;
    Node(int _x,int _y,int _num):x(_x),y(_y),num(_num) {}
};

int n;
pair<int,int>  newpoints[maxn];
vector<vector<Node> > plist;
int match[maxn];
bool vis[maxn];
int N;
int ans;
pair<int,int> points[maxn];

bool isCircle(int x,int y)
{
    while(x<plist[y].size())
    {
        if(vis[plist[y][x].num])
            return true;
        vis[plist[y][x].num]=true;
        pair<int,int> p=newpoints[match[plist[y][x].num]];
        x=p.first+1;
        y=p.second;
    }
    return false;
}
bool judge()
{
    for(int i=1; i<=n; ++i)
    {
        memset(vis,0,sizeof(vis));
        pair<int,int> p=newpoints[i];
        if(isCircle(p.first,p.second))
            return true;
    }
    return false;
}

void dfs(int cur)
{

    if(cur==N)
    {
        if(judge())
            ++ans;
        return ;
    }
    else
    {
        for(int i=1; i<=n; ++i)if(!match[i])
            {
                for(int j=i+1; j<=n; ++j)if(!match[j])
                    {
                        match[i]=j;
                        match[j]=i;
                        dfs(cur+1);
                        match[j]=0;
                    }
                match[i]=0;
                break;
            }
    }
}

void init()
{
    map<int,vector<int> > sortList;
    for(int i=1; i<=n; ++i)
        sortList[points[i].second].push_back(points[i].first);
    int id=0,y=0;
    for(map<int,vector<int> >::iterator it=sortList.begin(); it!=sortList.end(); ++it)
    {
        vector<int> &vec=it->second;
        sort(vec.begin(),vec.end());
        vector<Node> nlist;
        for(int i=0; i<vec.size(); ++i)
        {
            nlist.push_back(Node(i,y,++id));
            newpoints[id]=make_pair(i,y);
        }
        ++y;
        plist.push_back(nlist);
    }
}

/*
ID: kkkwjx1
PROG: wormhole
LANG: C++
*/
int main()
{
    freopen("wormhole.in","r",stdin);
    freopen("wormhole.out","w",stdout);
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
        scanf("%d%d",&points[i].first,&points[i].second);
    init();
    N=n/2;
    dfs(0);
    printf("%d\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值