#数据结构与算法学习笔记#PTA19:拯救007基础版 Saving James Bond - Easy Version(Java)

版权声明:本文为博主NJU_ChopinXBP原创文章,发表于CSDN,仅供交流学习使用,转载请私信或评论联系,未经博主允许不得转载。感谢您的评论与点赞。 https://blog.csdn.net/qq_20304723/article/details/83275030

2018.10.22

简单重述一下,James Bond被困在直径15的湖心岛上,整个湖可以模拟成一个100*100的矩阵,湖上有可供跳跃的陆地点(其实是鳄鱼背),每次007有一个最大的跳跃半径maxdist,问007能否成功逃脱跳到岸上。

这道题实际上是一道图题,但是却并不需要用邻接表或邻接矩阵建立一个图,只需要记录下所有可供跳跃的结点,把跳跃路径作为边,再进行一个简单的DFS,判断能够到达只需要一个简单的勾股定理即可。BFS的原理可以回顾:#数据结构与算法学习笔记#PTA18:图(邻接表)+DFS(深度优先搜索)+BFS(广度优先搜索)(C/C++)。需要注意的地方是由于湖心岛有15的直径,和其他结点不同,所以第一跳需要区分讨论。


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

Java实现:

/**
 * 
 * @author ChopinXBP
 * Saving James Bond - Easy Version 
 * 
 */

import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class SavingJamesBond_Easy {

	private static Scanner in = new Scanner(new BufferedInputStream(System.in));
	private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	private static int num;
	private static int maxdist;
	private static ArrayList<Area> crocodiles = new ArrayList<>();
	private static final int EDGE = 100;
	private static boolean canEscape = false;
	
	static class Area{
		Point location;
		boolean isJump = false;
		
		Area(int x, int y){
			location = new Point(x, y);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Read();	
		in.close();
		if(maxdist >= EDGE / 2 - 7.5){
			out.println("Yes");
		}
		else if(num <= 0 || maxdist <= 0){
			out.println("No");
		}
		else{
			DFSSolution(new Area(50, 50));
			if(canEscape){
				out.println("Yes");
			}else{
				out.println("No");
			}	
		}			
		out.close();
	}

	public static void Read(){
		String[] str = in.nextLine().split(" ");
		num = Integer.parseInt(str[0]);
		maxdist = Integer.parseInt(str[1]);
		
		for(int i = 0; i < num; i++){
			str = in.nextLine().split(" ");
			int x = Integer.parseInt(str[0]) + 50;
			int y = Integer.parseInt(str[1]) + 50;
			Area area = new Area(x, y);
			crocodiles.add(area);
		}
	}
	
	public static void DFSSolution(Area area){
		area.isJump = true;
		if(CanGetLand(area)) canEscape = true;
		if(canEscape) return;
		
		ArrayList<Area> canjumparea = CanJumpArea(area);
		for(int i = 0; i < canjumparea.size(); i++){
			if(canEscape) return;
			if(!canjumparea.get(i).isJump){
				DFSSolution(canjumparea.get(i));
			}
		}
	}
	
	public static ArrayList<Area> CanJumpArea(Area area){		
		ArrayList<Area> canjump = new ArrayList<>();		
		for(int i = 0; i < crocodiles.size(); i++){
			Area nextarea = crocodiles.get(i);
			//第一跳从湖心岛出发,湖心岛半径7.5
			if(area.location.x == 50 && area.location.y == 50){
				if(!nextarea.isJump && Math.pow(nextarea.location.x - area.location.x, 2)
						+ Math.pow(nextarea.location.y - area.location.y, 2) <= Math.pow(maxdist + 7.5, 2)){
					canjump.add(nextarea);
				}
			}else{
				if(!nextarea.isJump && Math.pow(nextarea.location.x - area.location.x, 2)
						+ Math.pow(nextarea.location.y - area.location.y, 2) <= Math.pow(maxdist, 2)){
					canjump.add(nextarea);
				}
			}
			
		}		
		return canjump;
	}
	
	public static boolean CanGetLand(Area area){
		if(area.location.x <= maxdist || area.location.y <= maxdist ||
				area.location.x >= EDGE - maxdist || area.location.y >= EDGE - maxdist){
			return true;
		}					
		return false;
	}
}

#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

猜你喜欢

转载自blog.csdn.net/qq_20304723/article/details/83275030