码迷,mamicode.com
首页 > 其他好文 > 详细

USACO Wormholes 【DFS】

时间:2015-01-27 21:31:15      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

描述

 

农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致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 方向走进来,虽然他不记得贝茜的当前位置。请帮助农夫约翰计算不同的虫洞配对(情况),使贝茜可能被困在一个无限循环中,如果她从不幸的位置开始。

[编辑]格式

PROGRAM NAME: wormhole

INPUT FORMAT:

(file wormhole.in)

第1行:N,虫洞的数目

第2到N+1行:每一行都包含两个空格分隔的整数,描述一个以(x,y)为坐标的单一的虫洞。每个坐标是在范围 0..1000000000。

OUTPUT FORMAT:

(file wormhole.out)

第1行:会使贝茜从某个起始点出发沿+x方向移动卡在循环中的不同的配对数。

[编辑]SAMPLE INPUT

4
0 0
1 0
1 1
0 1

[编辑]SAMPLE OUTPUT

2

[编辑]HINTS

[编辑]输入详细信息

有4个虫洞,在一个正方形角上。

[编辑]输出详细信息

如果我们将虫洞编号为1到4,然后通过匹配 1 与 2 和 3 与 4,贝茜会被卡住,如果她从(0,0)到(1,0)之间的任意位置开始或(0,1)和(1,1)之间。

| . . . .
4 3 . . .      贝茜会穿过B,A,
1-2-.-.-.      然后再次穿过B

相似的,在相同的起始点,贝茜也会陷入循环,如果配对是 1-3 和 2-4。

仅有1-4和2-3的配对允许贝西从任何二维平面上的点向+x方向走不出现循环。

 

不会做= =官方的代码:

/*
ID: wushuai2
PROG: wormhole
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 200000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;

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){
        int pos = start;
        for (int count = 0; count < N; ++count){
            pos = next_on_right[partner[pos]];
        }
        if (pos != 0) return true;  // does there exist a cylce starting from start
    }
    return false;
}

int solve(){                        // count all solutions
    int i, ans = 0;
    for (i = 1; i <= N; ++i)
        if (partner[i] == 0) break;  // find first unpaired wormhole
    if (i > N) {                     // everyone paired?
        if (cycle_exists()) return 1;
        else return 0;
    }
    for (int j = i + 1; j <= N; ++j) // try pairing i with all possible other wormholes j
        if (partner[j] == 0){
            partner[i] = j;  // try pairing i & j, let recursion continue to
            partner[j] = i;  // generate the rest of the solution
            ans += solve();
            partner[i] = partner[j] = 0;
        }
    return ans;
}

int main() {
    ofstream fout ("wormhole.out");
    ifstream fin ("wormhole.in");
    int i, j, k, t, m, s, c, w, q;
    memset(next_on_right, 0, sizeof(next_on_right));
    memset(partner, 0, sizeof(partner));
    fin >> N;
    for (i = 1; i <= N; ++i){
        fin >> X[i] >> Y[i];
    }
    for (i = 1; i <= N; ++i){ // set next_on_right[i]...
        for (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]){ //find next_on_right[i]
                    next_on_right[i] = j;
                }
            }
        }
    }
    fout << solve() << endl;
    //cout << solve() << endl;

    fin.close();
    fout.close();
    return 0;
}

 

USACO Wormholes 【DFS】

标签:

原文地址:http://www.cnblogs.com/wushuaiyi/p/4253840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!