【USACO】wormholes 【暴力】

题意:给出2K个平面上的点,给它们一一配对,问有多少种配对方法使得存在从某个点一直向右走会陷在循环里(K<=6)

思路:由于k很小,配对方法的话暴力枚举,然后判环,判环时需要注意的是一条直线上的四个点1,2,3,4 其中1和3配对,2和4配对,可以发现它不构成环,详见代码

/*{

ID:a4298442

PROB:wormhole

LANG:C++

}

*/

#include <stdio.h>

#include <iostream>

#include<fstream>

#include <string.h>

#include <algorithm>

#define maxn 1000

using namespace std;

ifstream fin("wormhole.in");

ofstream fout("wormhole.out");

struct T{int x;int y;}p[maxn];

int ans=0,g[maxn],match[maxn],n,visit[maxn];

int cmp(T x,T y){

return (x.y<y.y || ((x.y==y.y)&&(x.x<y.x)));

}

int circle(int u)

{

int vis[100]={0},v=g[u];

vis[u]=1;

while(v!=0){

u=match[v];

if(!u)return 0;v=g[u];

if(vis[u])return 1;vis[u]=1;

}

return 0;

}

void dfs(int k,int u,int num){

if(num==n){

for(int i=1;i<=n;i++)if(circle(i)){ans++;break;}

return;

}

visit[k]=1;

for(int i=(u==0)?k+1:1;i<=n;i++)if(visit[i]==0){

if(u==0){g[i]=k;g[k]=i;}

dfs(i,u^1,num+1);

if(u==0)g[i]=g[k]=0;else break;

}

visit[k]=0;

}

int main(){

fin>>n;

for(int i=1;i<=n;i++)fin>>p[i].x>>p[i].y;

sort(p+1,p+1+n,cmp);

for(int i=2;i<=n;i++)if(p[i].y==p[i-1].y)match[i-1]=i;

dfs(1,0,1);

fout<<ans<<endl;

return 0;

}

时间: 2024-08-26 14:04:50

【USACO】wormholes 【暴力】的相关文章

【USACO】Wormholes(暴力搜索)

直接按照题意暴力就行 /* 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

USACO Wormholes 【DFS】

描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他的虫洞将形成 N/2 连接配对.例如,如果A和B的虫洞连接成一对,进入虫洞A的任何对象体将从虫洞B出去,朝着同一个方向,而且进入虫洞B的任何对象将同样从虫洞A出去,朝着相同的方向前进.这可能发生相当令人不快的后果. 例如,假设有两个成对的虫洞A(1,1) 和 B(3,1),贝茜从(2,1)开始朝着 +x 方向(右)的位置移动.贝

USACO Wormholes(模拟)

题目请点我 题解: 这道题思路很简单,就是简单的深搜,找出所有的组合,然后判断能否成环.关键在于如何判断能否成环,我的思路是利用递归模拟,看能否第二次经过某一个点.中间也出现了错误,首先,每次访问的下一个点应该是同一行上当前点右边的第一个点:其次,某个点被访问过必须是作为起点被访问过,而不仅仅是到达. 代码实现: /* ID: eashion LANG: C++ TASK: wormhole */ #include <iostream> #include <cstdio> #inc

usaco Wormholes

有个数据能在我机子上跑对,在服务器上过不去,卡了我很久.后来才发现,有个数组大小少开了一个. 题意是,有头牛只会沿着x轴正方向走,但是y坐标随机.图上有很多虫洞,两两相连,从一个虫洞进去,从对应虫洞出来之后,牛还是会沿着x轴正方向走,让你统计虫洞两两相连的方式,其中会导致牛陷入循环的个数. 关于排列组合,两两相配的个数什么的还好,但是如何生成配对,编程什么的一开始我还真不会.以前也就自己写过全排列或者组合而已,没有写过这种. /* ID: modengd1 PROG: wormhole LANG

USACO ariprog 暴力枚举+剪枝

/* ID:kevin_s1 PROG:ariprog LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

HDU 4277 USACO ORZ(暴力+双向枚举)

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3809    Accepted Submission(s): 1264 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastu

[题解]USACO 1.3 Wormholes

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 coordin

USACO 1.3 Wormholes

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 coordin

USACO Ski Course Design 暴力

从Min到Max范围内暴力一下即可. /* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cma