USACO wormhole

洛谷 P1444 [USACO1.3]虫洞wormhole

https://www.luogu.org/problemnew/show/P1444

JDOJ 2386: USACO 2013 Dec Bronze 3.Wormholes

https://neooj.com:8082/oldoj/problem.php?id=2386

Description

Problem 3: Wormholes [Brian Dean, 2013]

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.  

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 (0,0) and B at (1,0), and that Bessie the cow starts from
position (1/2,0) moving in the +x direction.  Bessie will enter wormhole B,
exit from A, then enter B again, and so on, getting trapped in an infinite
cycle!

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.

Input

* 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.

Output

* 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 Input

4 0 0 1 0 1 1 0 1

Sample Output

2

HINT

 

INPUT DETAILS:

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

 

OUTPUT DETAILS:

If we number the wormholes 1..4, then by pairing 1 with 2 and 3 with 4,
Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or
between (0,1) and (1,1).  Similarly, with the same starting points, Bessie
can get stuck in a cycle if the pairings are 1-3 and 2-4.  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.

 
洛谷翻译的就是啥也不是。
 
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
int n,ans;
int v[20];
struct node
{
    int x,y;
}w[20];
bool cmp(node a,node b)
{
    if(a.y==b.y)
        return a.x<b.x;
    return a.y<b.y;
}
int find(int vis,int end,int begin,int way)
{
    if(vis!=1 && end==begin && way==1)
        return 1;
    if(way==0)
    {
        if(w[end].y==w[end+1].y)
            return find(vis+1,end+1,begin,1);
        return 0;
    }
    if(way==1)
        return find(vis+1,v[end],begin,0);
}
int judge()
{
    for(int i=1;i<=n;i++)
        if(find(1,i,i,1))
            return 1;
    return 0;
}
void dfs(int x)
{
    if(x==n+1)
    {
        if(judge())
            ans++;
        return;
    }
    if(v[x]==0)
    {
        for(int i=x+1;i<=n;i++)
            if(v[i]==0)
            {
                v[i]=x;v[x]=i;
                dfs(x+1);
                v[i]=v[x]=0;
            }
    }
    if(v[x]!=0)
        dfs(x+1);
    return;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&w[i].x,&w[i].y);
    sort(w+1,w+n+1,cmp);
    dfs(1);
    printf("%d",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/fusiwei/p/11217525.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值