07-图5 Saving James Bond - Hard Version(Java实现)

14 篇文章 0 订阅

This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape – he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).

Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

Output Specification:
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
Sample Output 1:
4
0 11
10 21
10 35
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
0

思路:

  1. 单源最短路径(BFS);
  2. 考虑多种特殊情况;
    (1) 鳄鱼在岸上或者在岛上;
    (2)一步直接迈上岸;
    (3)无法逃出升天;

代码如下:

import java.util.Scanner;

public class Main {

    //鳄鱼数量
    private Integer croNum;
    //跳的最远距离
    private Integer maxDis;

    public static void main(String[] args) {
        Main self = new Main();
        Dist[] croList = self.hanldInput();
        croList = self.save007(croList);
        //一步跳到岸上
        if (croList == null){
            System.out.println(1);
        }else {
            int result = -1;
            for (int i = 0; i < croList.length; i++) {
                if (croList[i].isSafe()){
                    if (result == -1){
                        result = i;
                    }else {
                        if (croList[i].getDist() < croList[result].getDist()){
                            result = i;
                        }else if(croList[i].getDist() == croList[result].getDist()){
                            //选择第一跳最近的路径
                            result = self.minFirstJump(croList, i, result);
                        }
                    }
                }
            }
            //不能逃出去
            if (result == -1){
                System.out.println(0);
            }else {
                System.out.println(croList[result].getDist() + 1);
                MyStack stack = new MyStack(croList.length);
                stack.push(result);
                while (croList[result].getPath() != -1){
                    result = croList[result].getPath();
                    stack.push(result);
                }
                while (stack.getLen() > 0){
                    System.out.println(croList[stack.pop()]);
                }
            }
        }
    }
    //选择第一跳最近的路径
    private int minFirstJump(Dist[] list, int d1, int d2){
        //d1的第一跳
        int t1 = d1;
        while (true){
            if (list[t1].getPath() == -1){
                break;
            }else {
                t1 = list[t1].getPath();
            }
        }
        //d1的第一跳
        int t2 = d2;
        while (true){
            if (list[t2].getPath() == -1){
                break;
            }else {
                t2 = list[t2].getPath();
            }
        }
        return list[t1].calDistFromO() > list[t2].calDistFromO() ? d2:d1;

    }


    //获得所有鳄鱼地址
    public Dist[] hanldInput() {
        Scanner input = new Scanner(System.in);
        this.croNum = input.nextInt();
        this.maxDis = input.nextInt();
        Dist[] croList = new Dist[this.croNum];
        for (int i = 0; i < this.croNum; i++) {
            Dist dis = new Dist(input.nextInt(), input.nextInt());
            croList[i] = dis;
        }
        return croList;
    }

    public Dist[] save007(Dist[] croList){
        Dist dist = new Dist(0,0,0);
        //直接跳到岸上
        if(dist.isSafe(this.maxDis)){
            return null;
        }else {
            for (int i = 0; i < croList.length; i++) {
                //刨除在岸上的鳄鱼
                if(!crocOnIsland(croList[i]) && dist.canJump(croList[i], this.maxDis)){
                    croList[i].setDist(croList[i].getDist() + 1);
                    croList = BFS(croList, i);
                }
            }
        }
        return croList;
    }

    //鳄鱼在岛上
    private boolean crocOnIsland(Dist dist){
        if (dist == null){
            return false;
        }
        if ( Math.abs(dist.getX()) <= 7.5 && Math.abs(dist.getY()) <= 7.5 ){
            return true;
        }
        return false;
    }
    //鳄鱼在岸上
    private boolean crocOnDest(Dist dist){
        if (dist == null){
            return false;
        }
        if (Math.abs(dist.getX()) >= 50 && Math.abs(dist.getY()) >= 50){
            return true;
        }
        return false;
    }

    public Dist[] BFS(Dist[] croList, int from){
        Queue queue = new Queue(this.croNum);
        queue.addQue(from);
        while (queue.getLen() > 0){
            int index = queue.leaveQue();
            if (croList[index].isSafe(this.maxDis)){
                croList[index].setSafe(true);
                croList[index].setDist(croList[index].getDist() + 1);
            }
            //找到邻接点
            for (int i = 0; i < croList.length; i++) {
                //刨除鳄鱼在岸上
                if ( croList[i].getDist() == -1 && !crocOnDest(croList[i]) && croList[index].canJump(croList[i], this.maxDis)){
                    croList[i].setDist(croList[index].getDist() + 1);
                    croList[i].setPath(index);
                    queue.addQue(i);
                }
            }
        }
        return croList;
    }



}

//鳄鱼节点
class Dist{
    private int x;
    private int y;
    int dist;
    int path;
    boolean safe;

    public Dist(int x, int y) {
        this.x = x;
        this.y = y;
        this.dist = -1;
        this.path = -1;
    }

    public Dist(int x, int y, int dist) {
        this.x = x;
        this.y = y;
        this.dist = dist;
    }

    public boolean canJump(Dist dis, int maxDis){
        double temp = Math.pow(this.x - dis.getX(), 2) + Math.pow(this.y - dis.getY(), 2);
        if(this.x == 0 && this.y == 0){
            return Math.sqrt(temp) <= maxDis + 7.5;
        }
        return Math.sqrt(temp) <= maxDis;
    }

    public double calDistFromO(){
        double temp = Math.pow(this.x, 2) + Math.pow(this.y, 2);
        return Math.sqrt(temp);
    }

    public boolean isSafe(int maxDis){
        if (Math.abs(this.x) + maxDis >= 50){
            return true;
        }
        if (Math.abs(this.y) + maxDis >= 50){
            return true;
        }
        return false;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDist() {
        return dist;
    }

    public void setDist(int dist) {
        this.dist = dist;
    }

    public int getPath() {
        return path;
    }

    public void setPath(int path) {
        this.path = path;
    }

    public boolean isSafe() {
        return safe;
    }

    public void setSafe(boolean safe) {
        this.safe = safe;
    }

    @Override
    public String toString() {
        return  x + " " + y;
    }
}
//队列
class Queue{
    private int[] que;
    private int top;
    private int rear;

    public Queue(int len) {
        this.que = new int[len];
        this.top = -1;
        this.rear = -1;
    }

    public void addQue(int node){
        que[++top] = node;
    }

    public int leaveQue(){
        return que[++rear];
    }

    public int getLen(){
        return top-rear;
    }

}
//堆栈类
class MyStack{
    private int cap;
    private Integer[] data;
    private int top;

    public MyStack(int cap) {
        this.cap = cap;
        this.top = -1;
        this.data = new Integer[cap];
    }
    //出栈
    public Integer pop(){
        if (this.top == -1){
            return null;
        }else{
            return data[this.top--];
        }
    }
    //入栈
    public boolean push(int num){
        if(this.top == this.cap - 1){
            return false;
        }else {
            data[++this.top] = num;
            return true;
        }
    }
    //获得栈顶元素
    public Integer getTopOne(){
        if (this.top == -1){
            return null;
        }else {
            return data[this.top];
        }
    }

    public int getLen(){
        return this.top+1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值