World Cup Fever(dfs+二维几何)
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 .
输入
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.
输出
样例输入 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;
}