Connect the Cows

Description

Every day, Farmer John walks around his farm to check on the health and well-being of his N (1 ≤ N ≤ 10) cows.

The location of each cow is described by a point in the 2D plane, and Farmer John starts out at the origin (0,0).  To make his route more interesting, Farmer John decides that he will only walk in directions parallel to the coordinate axes -- that is, only north, south, east, or west.  Furthermore, he only changes his direction of travel when he reaches the location of a cow (he may also opt to pass through the location of a cow without changing direction, if desired).  When he changes his direction of travel, he may make either a 90-degree or 180-degree turn.  FJ's route must take him back to the origin after visiting all his cows.

Please compute the number of different routes FJ can take to visit his N cows, if he changes direction exactly once at the location of each cow.  He is allowed to pass through the location of a cow without changing direction an arbitrary number of times.  The same geometric route taken forward versus backward counts as two different routes.

Input

多测试。

* Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains the x and y coordinates (space-separated) of the i-th point (each values is in the range -1000...1000).

Output

* Line 1: The number of different routes FJ can take (this could be zero if there are no valid routes).

Sample Input

4
0 1
2 1
2 0
2 -5

Sample Output

2

Hint

INPUT DETAILS:

There are 4 cows, at positions (0,1), (2,1), (2,0), and (2,-5).

OUTPUT DETAILS:

There are two different routes: Farmer John can visit cows in the orders 1-2-4-3 or 3-4-2-1 before returning to the origin.

Source
大概题意:在一个农场上,视为一个坐标系,而农夫有n头牛(n<=10),每头牛有相应的坐标,他需要从(0,0)的位置开始,往上下左右走,遍历每一头牛且只能一次,而且他一定要撞到一头牛之后才可以选择转弯,最后必须要回到原点的。在这里有一个坑是题目没有说清楚的,就是必须是经过牛时选择转弯了才可以算是经过这头牛
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int x[50],y[50],n,step[50],book;
int searc(int x1,int y1,int x2,int y2)
{
  if (x1!=x2&&y1!=y2)return -1;  
  if(x1==x2&&y1==y2)return -1;
  if (x1==x2&&y1<y2) return 0; 
  if (x1==x2&&y1>y2) return 1; 
  if (y1==y2&&x1<x2) return 2; 
  if (y1==y2&&x1>x2) return 3; 
}

void dfs(int num){
    if(book=0||num>n)
    return;
    if(searc(x[step[num]],y[step[num]],x[step[num+1]],y[step[num+1]])==-1||searc(x[step[num+1]],y[step[num+1]],x[step[num]],y[step[num]])==searc(x[step[num]],y[step[num]],x[step[num-1]],y[step[num-1]])){
        book=0;
        return;
    }
    dfs(num+1); 
}
int main()
{
    /*std::ios::sync_with_stdio(false);*/
    while(~scanf("%d",&n)){
        long long sum=0;
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
        for(int i=1;i<=n;i++)
        {
            cin>>x[i]>>y[i];
        }
        for(int i=1;i<=n;i++){
            step[i]=i;
        }
        book=1;
    do{
      dfs(0);
      sum+=book;
      book=1;
    }while(next_permutation(step+1,step+n+1));
    cout<<sum<<endl;
    }
    return 0;
 }

 

转载于:https://www.cnblogs.com/Yum20/p/9563539.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值