World Cup Fever(dfs+二维几何)

World Cup Fever(dfs+二维几何)

The  2018  World  CuP was  held  recently  in  Russia.  Some  great  soccer  countries  (e.g.,  Italy, Netherlands, Chile, USA) did not qualify for this World Cup.  These countries have found out that they needed more effective passing.

Given the player positions for two teams, determine the minimum number of passes needed to get the ball from one player to another player.  For the purposes of this problem, players do not change position, i.e., they do not move.

Player P1  can pass the ball directly to P2  if they are on the same team and no other player is in between the two players.

Let’s assume:
–  P1  and P2  are on the same team
–  P1 , P2 , P3  form a line with P3  between P1  and P2
–  There are no other players on the line formed by P1 , P2 , P3
Then,
–  If P3  is on the other team, P1  cannot pass the ball directly to P2 .
–  If P3  is on the same team, P1  can pass the ball to P3  to pass it to P2 .

输入

The first input line contains an integer, n (2 ≤  n ≤ 11), indicating the number of players on each team.  The second input line contains 2n integers, providing the (x,y) coordinates for the n players on Team 1; the first integer on this input line is the x coordinate for Player 1, the second integer is the y coordinate for Player 1, the third integer is the x coordinate for Player 2, etc.  The third input line provides (in a similar fashion) the (x,y) coordinates for the n players on Team 2.  Assume that all  coordinates  are  integers  between  1  and  999  (inclusive)  and  that  all  players  are  on  distinct locations, i.e., no two players occupy the same spot (point).

Assume Player 1 on Team 1 has the ball and wants to pass the ball to Player n on Team 1.  Assume that any player can pass the ball any distance.

输出

The output consists of a single integer, indicating the minimum number of passes needed to get the ball from Player 1 on Team 1 to Player n on Team 1.  If it is not possible to get the ball from Player 1 to Player n, print -1.

样例输入 Copy

3
10 15 13 17 10 19
10 17 16 17 13 19

样例输出 Copy

2

题意:求1-n最小的传球次数,n比较小,所以dfs。好像也可以最短路。

#pragma GCC optimize(3 , "Ofast" , "inline")
 
#include <bits/stdc++.h>
 
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define rop(i , a , b) for(register ll i=(a);i<(b);i++)
#define per(i , a , b) for(register ll i=(a);i>=(b);i--)
#define por(i , a , b) for(register ll i=(a);i>(b);i--)
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int > pi;
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
const int maxn = 1e5+10;
const int inf = int (1e9);
const ll INF = ll(1e18);
const double PI = acos (-1);
const int mod = 1e9+7;
const double eps = 1e-8;
 
struct point{
    int x,y;
}e1[20],e2[20];
int n,ans=inf;
int vis[20];
 
bool onSegment(point pi,point pj,point Q) {
    if((Q.x-pi.x)*(pj.y-pi.y)==(pj.x-pi.x)*(Q.y-pi.y)&&
    min (pi.x,pj.x)<=Q.x&&Q.x<=max (pi.x,pj.x)&&
    min (pi.y,pj.y)<=Q.y&&Q.y<=max (pi.y,pj.y)
    ){
        return true;
    }
    return false;
}
bool check(int p1,int p2) {
    rep (i,1,n) {
        if(onSegment(e1[p1],e1[p2],e2[i])) {
            return false;
        }
        if(i==p1||i==p2) continue;
        if(onSegment (e1[p1],e1[p2],e1[i])) {
            return false;
        }
    }
    return true;
}
 
void dfs(int p,int cnt) {
    vis[p]=1;
    if(p==n) {
        ans=min (ans,cnt);
        return;
    }
    rep (i,1,n) {
        if(vis[i]) continue;
        if(check(p,i)) {
            vis[i]=1;
            dfs (i,cnt+1);
            vis[i]=0;
        }
    }
}
int main(){
    read (n);
    rep (i,1,n) {
        read (e1[i].x);read (e1[i].y);
    }
    rep (i,1,n) {
        read (e2[i].x);read (e2[i].y);
    }
    dfs(1,0);
    if(ans==inf) ans=-1;
    cout<<ans<<endl;
    return 0;
}
发布了259 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dy416524/article/details/105734029