USACO-Section 1.3 Wormholes(枚举)

58 篇文章 0 订阅

Wormholes

Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).

According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.

For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
   | A > B .      Bessie will travel to B then
   + . . . .      A then across to B again

Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.

Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn't know which wormhole pairs with any other wormhole, so find all the possibilities.

PROGRAM NAME: wormhole

INPUT FORMAT:

Line 1:The number of wormholes, N.
Lines 2..1+N:Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT (file wormhole.in):
4
0 0
1 0
1 1
0 1

INPUT DETAILS:

There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT:

Line 1:The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT (file wormhole.out):

2

OUTPUT DETAILS:

If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . . .
   4 3 . . .      Bessie will travel to B then
   1-2-.-.-.      A then across to B again

Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 1-3 and 2-4 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).

Only the pairings 1-4 and 2-3 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling. 


数据很小,但是我不剪枝的枚举还是超时了。。。

发现官方题解真的很巧妙,每次枚举一对黑洞时,找第一个未配对的黑洞,这样绝对不会重复。

/*
ID: your_id_here
PROG: wormhole
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int i,ans,n,nn,nnn,rig[12],par[12],cur,fac[6]={1,1,2,6,24,120};
bool vis[12];

struct Node {
    int x,y;
    bool operator < (const Node& a) const {
        return y<a.y||(y==a.y&&x<a.x);
    }
}p[13];

bool judge() {
    int i;
    for(i=0;i<n;++i) {
        memset(vis,false,sizeof(vis));
        cur=i;
        while(true) {
            cur=rig[cur];
            if(cur==-1)
                break;
            cur=par[cur];
            if(vis[cur])
                return true;
            vis[cur]=true;
        }
    }
    return false;
}

void dfs(int num) {
    if(num==nn) {
        if(judge())
            ++ans;
        return ;
    }
    int i,j;
    for(i=0;i<n;++i) {
        if(num==0&&i>0)//特判是否在找第一对虫洞,如果是第一对虫洞并且都不是第一个虫洞则返回,防止重复计算(不要这个竟然超时...)
            return;
        if(par[i]==-1) {
            for(j=i+1;j<n;++j)//j>i防止i和j一对与j和i一对同时出现,变排列为组合
                if(par[j]==-1) {
                    par[i]=j,par[j]=i;
                    dfs(num+1);
                    par[i]=par[j]=-1;
                }
        }
    }
}

int main() {
    freopen("wormhole.in","r",stdin);
    freopen("wormhole.out","w",stdout);
    while(1==scanf("%d",&n)) {
        for(i=0;i<n;++i)
            scanf("%d%d",&p[i].x,&p[i].y);
        nn=n>>1,nnn=nn-1;
        sort(p,p+n);
        p[n].x=p[n].y=-1;//刚开始数组开小1个,导致越界,但是不提示,只说答案错误,狂交20次才发现...
        for(i=0;i<n;++i) {//预处理所有点往右走能走到的点
            par[i]=-1;
            if(p[i].y!=p[i+1].y)
                rig[i]=-1;
            else
                rig[i]=i+1;
        }
        ans=0;
        dfs(0);
        printf("%d\n",ans/fac[nnn]);//高中数学还记得,将m个物品n等分,最后需要除以A(n,n)=n!
    }
    return 0;
}

贴一下官方标程:

#include <iostream>
#include <fstream>
using namespace std;
#define MAX_N 12

int N, X[MAX_N+1], Y[MAX_N+1];
int partner[MAX_N+1];
int next_on_right[MAX_N+1];

bool cycle_exists(void)
{
  for (int start=1; start<=N; start++) {
    // does there exist a cylce starting from start
    int pos = start;
    for (int count=0; count<N; count++)
      pos = next_on_right[partner[pos]];
    if (pos != 0) return true;
  }
  return false;
}

// count all solutions
int solve(void) 
{
  // find first unpaired wormhole
  int i, total=0;
  for (i=1; i<=N; i++) 
    if (partner[i] == 0) break;

  // everyone paired?
  if (i > N) {
    if (cycle_exists()) return 1;
    else return 0;
  }

  // try pairing i with all possible other wormholes j
  for (int j=i+1; j<=N; j++)
    if (partner[j] == 0) {
      // try pairing i & j, let recursion continue to 
      // generate the rest of the solution
      partner[i] = j;
      partner[j] = i;
      total += solve();
      partner[i] = partner[j] = 0;
    }
  return total;
}

int main(void)
{
  ifstream fin("wormhole.in");
  fin >> N;
  for (int i=1; i<=N; i++) fin >> X[i] >> Y[i];
  fin.close();
  
  for (int i=1; i<=N; i++) // set next_on_right[i]...
    for (int j=1; j<=N; j++)
      if (X[j] > X[i] && Y[i] == Y[j]) // j right of i...
	if (next_on_right[i] == 0 ||
	    X[j]-X[i] < X[next_on_right[i]]-X[i])
	  next_on_right[i] = j;

  ofstream fout("wormhole.out");
  fout << solve() << "\n";
  fout.close();
  return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值