PTA数据结构-06-图2 Saving James Bond - Easy Version

一、题目

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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No

二、解答

import java.util.*;
//此处的map代表图,而不是键值对
public class Main{
    static ArrayList<Integer> stack = new ArrayList<>();
    static int index = -1;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean res = DFS(creatMap(sc));
        sc.close();
        System.out.println(res ? "Yes":"No");
    }

    public static boolean DFS(int[][] map){
        int nodeNum = map.length;
        int[] visited = new int[nodeNum];
        int node = 0;
        while (node != -1){
            if (map[node][nodeNum-1] == 1) return true;
            if (visited[node] == 1) { node = pop(); continue;}
            //可以直接免去i=0和i=nodeNum-1两个点的判断
            for (int i = 1; i < nodeNum - 1; i++) {
                if (map[node][i] == 1 && visited[i] == 0) push(i);
            }
            visited[node] = 1;
            node = pop();
        }
        return false;
    }
    public static int[][] creatMap(Scanner sc){
        int nodeNum = sc.nextInt();
        int maxDis = sc.nextInt();
        int [][] posMap = new int[nodeNum][2];
        int [][] judgeMap = new int[nodeNum+2][nodeNum+2];//算上岸边和出发点
        for (int i = 0; i < nodeNum; i++) {
            posMap[i][0] = sc.nextInt();
            posMap[i][1] = sc.nextInt();
        }
        judgeEdge(maxDis, posMap, judgeMap);
        judgeDis(maxDis, posMap, judgeMap);
        return judgeMap;
    }
    public static void judgeEdge(int m, int[][] posMap, int[][] judgeMap){
        int edgeIndex = posMap.length + 1; //将岸边放在数组最后一个,出发点放在数组第一位
        int startIndex = 0;
        for (int i = 0; i < posMap.length; i++) {
            int x = posMap[i][0], y = posMap[i][1];
            //判断与岸边距离
            if (x + m >= 50 || x - m <= -50 || y + m >= 50 || y - m <= -50) {
                judgeMap[i+1][edgeIndex] = 1;
                judgeMap[edgeIndex][i+1] = 1;
            }
            //判断与出发点距离
            int dis = (m + 15) * (m + 15);
            if (x*x + y*y <= dis) {
                judgeMap[startIndex][i+1] = 1;
                judgeMap[i+1][startIndex] = 1;
            }
        }
    }
    public static void judgeDis(int m, int[][] posMap, int[][] judgeMap){
        //judge数组的第一个元素和数组最后一个元素已经进行判断
        for (int i = 0; i < posMap.length; i++) {
            int x1 = posMap[i][0], y1 = posMap[i][1];
            for (int j = 0; j < posMap.length; j++) {
                int x2 = posMap[j][0], y2 = posMap[j][1];
                if ((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) <= m * m) {
                    judgeMap[i+1][j+1] = 1;
                    judgeMap[j+1][i+1] = 1;
                }
            }
        }
    }
    public static void push(int num){
        stack.add(num);
        index++;
    }
    public static int pop(){
        if (index < 0) return -1;
        return stack.remove(index--);
    }

}

思路:

  1. 考虑如何将复杂的坐标位置图转为简单的图:  计算每两个点之间的距离,跟最大距离比较,满足则连通,不满足则不连通
  2. 考虑如何上岸,如何离岸: 将岸也作为图上的点,将出发岛也作为图上的点
  3. 建立起图后考虑如何遍历图: DFS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值