[USACO1.3]虫洞wormhole 搜索+模拟

22 篇文章 0 订阅
14 篇文章 0 订阅

题目

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 (i.e., all the different ways that N wormholes could be paired such that Bessie can, in some way, get in a cycle). Note that a loop with a smaller number of wormholes might contribute a number of different sets of pairings to the total count as those wormholes that are not in the loop are paired in many different ways.

农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点。

根据他的计算,约翰知道他的虫洞将形成 N/2 连接配对。例如,如果A和B的虫洞连接成一对,进入虫洞A的任何对象体将从虫洞B出去,朝着同一个方向,而且进入虫洞B的任何对象将同样从虫洞A出去,朝着相同的方向前进。这可能发生相当令人不快的后果。

例如,假设有两个成对的虫洞A(1,1) 和 B(3,1),贝茜从(2,1)开始朝着 +x 方向(右)的位置移动。贝茜将进入虫洞 B(在(3,1)),从A出去(在(1,1)),然后再次进入B,困在一个无限循环中!

| . . . .
| A > B . 贝茜会穿过B,A,

  • . . . . 然后再次穿过B
    农夫约翰知道他的农场里每个虫洞的确切位置。他知道贝茜总是向 +x 方向走进来,虽然他不记得贝茜的当前位置。请帮助农夫约翰计算不同的虫洞配对(情况),使贝茜可能被困在一个无限循环中,如果她从不幸的位置开始。

题解

枚举搜出所有可能的虫洞组合方案,然后用模拟(搜索?)判断这样的组合会不会导致无限循环。

代码

/*
ID: yjy_aii1
TASK: wormhole
LANG: C++
*/
#include <cstdio>
#include <cstring>

using namespace std;

int n,ans,cnt;
int bz[15],f[15];
bool b[15];
int x[15],y[15];

bool trya(int d,int k,int be,int to){
	if ((d!=1)&&(k==be)&&(to==0))
		return true;
	if (to==1){
		int c=1000000001,e=0;
		for (int i=1;i<=n;i++){
			if (x[k]==x[i]&&y[i]>y[k]&&y[i]<c)
				{c=y[i];e=i;}
		}
		if (e)
		return trya(d+1,e,be,0);
		return false;
	}
	if (to==0){
		return trya(d+1,f[k],be,1);
	}
}

void ok(){
	int fl=0;	
	cnt=0;
	for (int i=1;i<=n;i++)
	{
		memset(bz,0,sizeof(int)*13);
		if (trya(1,i,i,0)){
			fl=1;
			break;
		}
	}
	if (fl) 
	ans++;
}

void dfs(int d){
	if (d==n/2){
		ok();
		return;
	}
	int st=0;
	for (int i=1;i<n;i++)
		if (!b[i]){
			st=i;b[i]=1;break;
		}
	for (int i=st+1;i<=n;i++)
		if (!b[i]){
			f[st]=i;f[i]=st;
			b[i]=1;
			dfs(d+1);
			b[i]=0;
		}
	b[st]=0;
}

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",&y[i],&x[i]);
	dfs(0);
	printf("%d\n",ans);
}

花谢满城可堪怜

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值