2014
02-14

# Colliding Traffic

For a boat on a small, constrained body of water, other traffic can be a major hazard. The more traffic there is in the same area, the higher the risk of a collision.

Your job is to monitor traffic and help detect likely collisions before they occur. You have sensors to detect the position, direction, and speed of each boat. Assuming the direction and speed remain constant, your task is to determine whether any of the boats will collide. Two boats are considered to collide if they come within a given distance of each other.

The first line of each test chunk contains a single integer c, the number of test cases to follow. Each test case starts with a line containing two numbers, n, the number of boats, and r, the collision distance. Two boats are considered to collide if they come within r metres of each other. There will be no more than 1000 boats. Each boat is identified by a line containing four numbers x, y, d, s. The numbers x and y give the current position of the boat as a distance east and north, respectively, from a common origin, and will be between -1000 and 1000, inclusive. The lake is small enough that we can model it as a flat surface. The number d gives the direction in which the boat is heading in degrees clockwise from north (so east is 90 degrees). The number s gives the speed of the boat in metres per second, and will be between 0.001 and 1000. Note that r, x, y, d, and s are not necessarily integers. The input data will be such that the answer will not change if any of the numbers x, y, d and s are changed by 10^-6 or less.

Please process to the end of the data file.

The first line of each test chunk contains a single integer c, the number of test cases to follow. Each test case starts with a line containing two numbers, n, the number of boats, and r, the collision distance. Two boats are considered to collide if they come within r metres of each other. There will be no more than 1000 boats. Each boat is identified by a line containing four numbers x, y, d, s. The numbers x and y give the current position of the boat as a distance east and north, respectively, from a common origin, and will be between -1000 and 1000, inclusive. The lake is small enough that we can model it as a flat surface. The number d gives the direction in which the boat is heading in degrees clockwise from north (so east is 90 degrees). The number s gives the speed of the boat in metres per second, and will be between 0.001 and 1000. Note that r, x, y, d, and s are not necessarily integers. The input data will be such that the answer will not change if any of the numbers x, y, d and s are changed by 10^-6 or less.

Please process to the end of the data file.

2
2 5
0 0 90 1
10 10 180 1
2 10
0 0 0 0
8 8 270 1
2
2 5
0 0 90 1
10 10 180 1
2 10
0 0 0 0
8 8 270 1

6
2
6
2

//
//#tag
//#sol

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <sstream>
#include <climits>

// #include <unordered_map>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pdd pair<double,double>
#define F first
#define S second
#define REP(i,j,k) for(int (i)=(j);(i)<(k);++(i))
#define pb push_back
#define PI acos(-1)
#define db(x) cerr << #x << " = " << x << endl;
#define _ << ", " <<
#define mp make_pair
#define FILL(x,y) memset(x,y,sizeof(x))
#define EPS 1e-9
#define FOREACH(i,v) for(typeof((v).begin()) i=(v).begin();i!=(v).end();i++)

// #define umap unordered_map

using namespace std;

template <class _T> inline string tostr(const _T& a){ ostringstream os(""); os<<a;return os.str();}

int n; double r;

double x[1010];
double y[1010];
double d[1010];
double s[1010];

int colision( int A, int B){
double cx = x[B] - x[A];
double cy = y[B] - y[A];
double v1x = sin(d[A]) * s[A];
double v1y = cos(d[A]) * s[A];
double v2x = sin(d[B]) * s[B];
double v2y = cos(d[B]) * s[B];
double vx = v2x - v1x;
double vy = v2y - v1y;
// db( cx _ cy _ v1x _ v1y _ v2x _ v2y );
double deno = 2 * (vx * vx) + 2 * (vy * vy);

double nomi = -1.0 * (2.0 * cx* vx + 2.0 * cy * vy);
double t = nomi / deno;

double ox = cx + vx * t;
double oy = cy + vy * t;
double dist = sqrt( ox*ox + oy*oy);

if( fabs(deno) < EPS || t < EPS){
double dist = sqrt( cx*cx + cy*cy);
if( dist <= r + EPS){
return 0;
} else {
return 1000000000;
}
}
if( dist <= r + EPS ){
// binary search from 0 to t
double ini = 0, end = t, mid = 0;
int cont = 100;
while(cont--){
mid = (ini + end)/2.0;
double ox = cx + vx * mid;
double oy = cy + vy * mid;
double dist = sqrt( ox*ox + oy*oy);
if( dist < r + EPS){
end = mid;
} else {
ini = mid;
}
}
return round(mid);
} else {
return 1000000000;
}
}

int main(){
int T; cin >> T;
while(T--){
cin >> n >> r;
REP(i,0,n){
scanf("%lf%lf%lf%lf", &x[i], &y[i], &d[i], &s[i]);
d[i] *= PI/180.0;
}
int resp = 1000000000;
REP(i,0,n){
REP(j,i+1,n){
resp = min ( resp , colision(i,j));
}
}
if( resp == 1000000000){
puts("No collision.");
} else {
printf("%d\n", resp);
}
}
return 0;
}

1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count