Wormholes USACO

先给个任意门:

http://train.usaco.org/usacoprob2?a=Hjk3nSx2aDB&S=wormhole

又是一道好题
直接解释吧~

x,y -> position
r[u] -> 第u个点右边第一个hole的位置
partner[u] -> 与u相连的点

Then 怎么判断她就陷入循环了呢?
也就是以下代码中n-1次判断走过虫洞并判断虫洞。

pos表示从第pos个虫洞进入,进入后,我们自然就到达了 partener [pos]
那下一个位置就是 r[ partener[pos] ]
所以 pos=r[ partener[pos]] 即可。实现不断的循环了!

then 这样循环n-1次之后 判断pos 是否为0,为0就表示并没有循环,已经成功走出去了~

不是0就当然代表已经遭定了。本题主体就是这样了。

宝贵的一点,本题用到的回溯相当的巧妙,细细enjoy以下.~

下附代码:

#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <string.h>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("wormhole.in");
ofstream fout("wormhole.out");
int n, res;
int partner[14];
int x[14], y[14], r[14];

bool cycle_exist() {
    for (int i = 1; i <= n; i++) {
        int pos = i;
        for (int j = 1; j < n; j++) {
            pos = r[partner[pos]];
        }
        if (pos != 0) return true;
    }
    return false;
}

int solve() {
    int i; int total = 0;
    for (i = 1; i <= n; i++) {
        if (partner[i] == 0) break;
    }

    if (i >= n) {
        if (cycle_exist()) return 1;
        else return 0;
    }

    for (int j = i+1; j <= n; j++) {
        if (partner[j] == 0) {
            partner[i] = j;
            partner[j] = i;
            total += solve();
            partner[i] = partner[j] = 0;
        }
    }
    return total;
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> x[i] >> y[i];
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (x[j] > x[i] && y[j] == y[i]) {
                if (r[i] == 0 || x[j] - x[i] < x[r[i]] - x[i]) {
                    r[i] = j;
                }
            }
        }
    }

    fout << solve() << endl;
    return 0;
}
时间: 2024-08-09 06:34:33

Wormholes USACO的相关文章

[题解]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 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 Section1.3 Wormholes 解题报告

wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 一个人在二维坐标系上走,方向永远是+x.此坐标系中有N个虫洞(N是偶数). 虫洞这东西,一旦两个配成一对,便可以形成“传送门

USACO 1.3 Wormholes (不会做,贴官方解答)

描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致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: 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

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

【USACO 1.3】Wormholes

/* LANG: C++ TASK: wormhole n个洞,n<=12, 如果两洞配对,则它们之间有地下路径(无向) 牛在地上只会往+x方向 问多少种两两配对的方案,牛从地上某位置出发,会陷入无限循环. SOLVE: 枚举所有配对方案,判断是否会进入无限循环. */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define N 15 int n,