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

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

题意:给出各个顶点的坐标,要求判断在给出的跳跃半径下,能否达到岸边位置
解题思路:根据给出的顶点坐标,建立图中各个顶点的连通关系(根据两个顶点的距离是否小于给出跳跃半径判断两个顶点是否连通),再从原点开始进行遍历,当存在能够到达岸边的顶点时,返回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 stub
		
		int 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");
		else
			System.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值