PAT:Saving James Bond - Easy Version (Java实现)

来源:互联网 发布:淘宝旧版本ipad 编辑:程序博客网 时间:2024/06/07 02:03

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 2025 -15-25 288 4929 15-35 -25 2827 -29-8 -28-20 -35-25 -20-13 29-30 15-35 4012 12
Sample Output 1:
Yes
Sample Input 2:
4 13-12 1212 12-12 -1212 -12
Sample Output 2:
No

题意:给出各个顶点的坐标,要求判断在给出的跳跃半径下,能否达到岸边位置
解题思路:根据给出的顶点坐标,建立图中各个顶点的连通关系(根据两个顶点的距离是否小于给出跳跃半径判断两个顶点是否连通),再从原点开始进行遍历,当存在能够到达岸边的顶点时,返回YES,否则返回NO。


Java代码如下:

import java.util.Scanner;/** * Saving James Bond * 浙大数据数据结构(陈越)——图课后题 * @author Jacob */class Point {double x;double y;Point(double x,double y){this.x=x;this.y=y;}}public class SavingJamesBond {public static double RADIUS = 15 / 2;public static void main(String[] args) {// TODO Auto-generated method stubint N_MAX = 100;Scanner sc = new Scanner(System.in);int N = sc.nextInt();if (N > N_MAX) {System.out.println("N can not larger than 100");System.exit(0);}double JUMP_DISTANCE = sc.nextInt();Point[] point = new Point[N];int[] visited = new int[N];for (int i = 0; i < N; i++) {Point p=new Point(sc.nextDouble(),sc.nextDouble());point[i]=p;}// 第一个点要单独处理,不能递归boolean answer=false;   //用来记录最终结果for (int i = 0; i < N; i++) {if (firstJump(point[i],JUMP_DISTANCE) && visited[i] == 0) {visited[i]=1;if(answer=dfs(point,i,visited,N,JUMP_DISTANCE))break;}}if(answer==true)System.out.println("YES");elseSystem.out.println("NO");sc.close();}/** * 第一次跳跃 * @param point * @param RADIUS * @param JUMP_DISTANCE * @return */public static boolean firstJump(Point point,double JUMP_DISTANCE){if(Math.sqrt(point.x*point.x+point.y*point.y)<RADIUS+JUMP_DISTANCE)return true;return false;}/** * 对深度优先搜索进行修改 * @param point * @param root * @param visited * @param N * @param JUMP_DISTANCE * @return */public static boolean dfs(Point[] point,int root,int[] visited,int N,double JUMP_DISTANCE) {if(isSafe(point[root],JUMP_DISTANCE))return true;boolean answer=false;  // answer用来记录该函数返回结果for(int i=0;i<N;i++){if(visited[i]==0 && canJump(point[root],point[i],JUMP_DISTANCE)){visited[i]=1;answer=dfs(point,i,visited,N,JUMP_DISTANCE);}}return answer;}/** * 判断两个点是否能进行跳跃 * @param root1 * @param root2 * @param JUMP_DISTANCE * @return */public static boolean canJump(Point root1,Point root2,double JUMP_DISTANCE){double distance=Math.abs(Math.sqrt(root1.x*root1.x+root1.y*root1.y)-Math.sqrt(root2.x*root2.x+root2.y*root2.y));if(distance<JUMP_DISTANCE)return true;return false;}/** * 判断是否安全,不需要再计算与(-50,-50),(-50,50),(50,-50),(50,50)的距离 * 用Math.abs(point[i].x)<50-jump || Math.abs(point[i].y)<50-jump * @param point * @param i * @param jump * @return */public static boolean isSafe(Point point,double JUMP_DISTANCE) {if (Math.abs(point.x) > 50 - JUMP_DISTANCE || Math.abs(point.y) > 50 - JUMP_DISTANCE)return true;return false;}}




0 0
原创粉丝点击