Problem Statement
Circles Country is a country that contains several circular-shaped districts. Some districts may be situated inside other districts, but their borders do not intersect or touch.
Qatam is a resident of Circles Country. When he travels between two locations, he always tries to cross the fewest number of district borders as possible because crossing borders is usually a laborious task.
Imagine Circles Country as an infinite plane. You are given int x[] and int y[] and int r[], where (x[i],y[i]) are the coordinates of the ith district's center and r[i] is its radius.
There are n circles altogether. Qatam is currently at point (x1,y1) and he needs to get to point (x2,y2).
Neither of these points lies on a district border. Return the minimal number of district borders he must cross to get to his destination.
Constraints
n will be between 1 and 50, inclusive.
x, y and r will each contain n elements.
Each element of x and y will be between -1000 and 1000, inclusive.
Each element of r will be between 1 and 1000, inclusive. x1, y1, x2 and y2 will be between -1000 and 1000, inclusive.
No two circumferences will have common points. The points (x1,y1) and (x2,y2) will not lie on any of the circumferences.
My Code: int least_borders(int x[], int y[], int r[], size_t n, int x1, int y1, int x2, int y2) { // fill in code here int borders = 0; int counter = 0; for (int i = 0 ; i < n; i++){ if (x1 < x[i]+r[i] && x1 > x[i]-r[i]){ if(y1 < y[i] + r[i] && y1 > y[i] - r[i]){ counter++; if (x1 < x[i]+r[i] && x1 > x[i]-r[i] && x2 < x[i]+r[i] && x2 > x[i]-r[i]){ if(y1 < y[i] + r[i] && y1 > y[i] - r[i] && y2 < y[i] + r[i] && y2 > y[i] - r[i]){ counter = counter - 1; } } } } if (x2 < x[i]+r[i] && x2 > x[i]-r[i]){ if(y2 < y[i] + r[i] && y2 > y[i] - r[i]){ counter++; if (x1 < x[i]+r[i] && x1 > x[i]-r[i] && x2 < x[i]+r[i] && x2 > x[i]-r[i]){ if(y1 < y[i] + r[i] && y1 > y[i] - r[i] && y2 < y[i] + r[i] && y2 > y[i] - r[i]){ counter = counter - 1; } } } } } borders = counter; return borders; }