Saving James Bond - Hard Version
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int N, D;
int path[105];
int disk[105];
struct Node{
int x;
int y;
} Pos[105];
bool judge(Node a, Node b){
int c1 = a.x * a.x + a.y * a.y;
int c2 = b.x * b.x + b.y * b.y;
return c1 < c2;
}
void init(){
for(int i = 1; i <= N; i++){
path[i] = 0;
disk[i] = 0;
}
}
int FirstJump(int i){
if((7.5 + D) * (7.5 + D) >= Pos[i].x * Pos[i].x * 1.0 + Pos[i].y * Pos[i].y)
return 1;
return 0;
}
int jump(int i, int j){
if((Pos[i].x - Pos[j].x) * (Pos[i].x - Pos[j].x) + (Pos[i].y - Pos[j].y) * (Pos[i].y - Pos[j].y) <= D * D)
return 1;
return 0;
}
int isSafe(int i){
if(abs(Pos[i].x) + D >= 50 || abs(Pos[i].y) + D >= 50)
return 1;
return 0;
}
void SaveOO7(){
queue<int> q;
for(int i = 1; i <= N; i++){
if(FirstJump(i)){ //未考虑在岛上
q.push(i);
disk[i] = 1;
}
}
int v;
while(!q.empty()){
v = q.front();
q.pop();
for(int j = 1; j <= N; j++){
if(jump(v, j) && disk[j] == 0){
q.push(j);
disk[j] = disk[v] + 1;
path[j] = v;
}
}
}
}
int Min(int a, int b){
while(path[a] != 0){
a = path[a];
}
while(path[b] != 0){
b = path[b];
}
if(a < b) return 1;
return 0;
}
int main()
{
cin >> N >> D;
for(int i = 1; i <= N; i++){
cin >> Pos[i].x >> Pos[i].y;
}
sort(Pos + 1, Pos + 1 + N, judge);
if(7.5 + D >= 50){
cout << 1;
}
else{
init();
SaveOO7();
int Disk = 1000;
int Safe = 0;
for(int i = 1; i <= N; i++){
if(isSafe(i) && (Disk > disk[i] + 1) && disk[i] > 0){ //看的是头;
Disk = disk[i] + 1;
Safe = i;
}
if(isSafe(i) && (Disk == disk[i] + 1) && disk[i] > 0){
if(Min(i,Safe)){
Disk = disk[i] + 1;
Safe = i;
}
}
}
if(Disk != 1000) cout << Disk << endl;
else{
cout << 0;
return 0;
}
stack<int> s;
s.push(Safe);
while(path[Safe] != 0){
Safe = path[Safe];
s.push(Safe);
}
while(!s.empty()){
cout << Pos[s.top()].x << " " << Pos[s.top()].y << endl;
s.pop();
}
}
}