【PTA】

 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();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值