Saving James Bond - Hard Version (java)

7-11 Saving James Bond - Hard Version(30 分)

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的基础上增加记录路径长度的数组dist[N],和记录经过节点的数组path[N]。把原点也加进去的话就是N+1.

2、由于原点和其他点判断邻接点的方法不一样,所以要分开写个if else 语句。007开始迈的步子最大。

3、dist和path路径更新完毕后,筛选dist不为-1,并且可以一步跳到岸上的点。然后在满足要求的节点中再继续筛选dist[i]最小的节点,如果有多个最小的节点,就存在一个ArrayList中,分别压入stack里,判断第一步哪个节点离007最近。

4、 测试点5就是,如果007可以一步迈上岸的情况,不需要踩鳄鱼,直接输出1.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;




public class Main {
	
	static int N;
	static int D;
	static double  dia = 7.5;
	static Node [] node;
	static int [] dist;
	static int [] path;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		N = in.nextInt();
		D = in.nextInt();
		node =new Node[N+1];
		dist = new int[N+1];
		path = new int [N+1];
		
		for(int i = 0; i<N+1;i++) {
			dist[i] = -1;
			path[i] = -1;
		}
		dist[0] = 0;
		
		node[0] = new Node(0,0);
		for(int i = 1 ;i<N+1;i++) {
			node[i] = new Node(in.nextInt(),in.nextInt());
		}
		if(D<(50-dia)) {
				unweight(0);
				ArrayList<Integer> al = new ArrayList<>();
				int temp = Integer.MAX_VALUE;
				for(int i = 0;i<N+1;i++) {
					double dx = 50 - Math.abs(node[i].x);
					double dy = 50 - Math.abs(node[i].y);
					if((dx<=D||dy<=D)&&dist[i]!=-1) {
						al.add(i);
						if(dist[i]<temp)
							temp = dist[i];									//temp存下dist数组中的最小值。
					}
				}
				
				ArrayList<Integer> xx = new ArrayList<>();
				
				for(int i =0; i<al.size();i++) {
					if(dist[al.get(i)]<=temp)
						xx.add(al.get(i));
				}
		
				if(al.isEmpty())
					System.out.print(0);
				else {
					printpath(xx);
				}
		}
		else
				System.out.print(1);
		
	}
		private static void printpath(ArrayList<Integer> al) {
		// TODO Auto-generated method stub
		int n = al.size();
		Stack<Integer> tmp = null;
		double len;
		double temp = Double.MAX_VALUE;
		for(int i = 0; i<n;i++) {
			Stack<Integer> st = print(al.get(i));
			len =distance (st.peek(),0);
			if(len<temp) {
				tmp = st;
				temp = len;
			}
		}
		
		int step = tmp.size()+1;
		System.out.println(step);
		while(!tmp.isEmpty()) {
			int i = tmp.pop();
			System.out.println(node[i].x+" "+node[i].y);
		}
		
			
	}
		private static Stack<Integer> print(Integer i) {
			// TODO Auto-generated method stub
			Stack<Integer> st = new Stack<>();
			st.push(i);
			int temp=i;
			do{
				if(path[temp]!=0) {
					st.push(path[temp]);
				}
				temp = path[temp];
			}while(temp!=0);
			
			return st;
		}
		public static void unweight(int vertex) {
			Queue<Integer> qu = new LinkedList<>();
			qu.add(vertex);
			int flag = 0;
			while(!qu.isEmpty()) {
				int tmp = qu.poll();
				if(flag==0) {											//对于原点而言,因为第一次跳的远、
					for(int i = 1;i<N+1;i++) {
						if(distance(tmp,i)<=(D+dia)) {
							dist[i] = dist[0]+1;
							path[i] = tmp;
							qu.add(i);
						}
					}
					flag = 1;
				}
				
				else {													//对于其他节点,只有if语句里判断条件不同
					for(int i =0;i<N+1;i++) {
						if(dist[i]==-1&&distance(tmp,i)<=D) {
							dist[i] = dist[tmp]+1;
							path[i] = tmp;
							qu.add(i);
						}
					}
				}	
			}
			
		}
		private static double distance(int tmp, int i) {
			// TODO Auto-generated method stub
			int x1 = node[tmp].x;
			int y1 = node[tmp].y;
			int x2 = node[i].x;
			int y2 = node[i].y;
			
			double dx = Math.pow(x1-x2, 2);
			double dy = Math.pow(y1-y2, 2);
			
			double result = Math.sqrt(dx+dy);
			
			return result;
		}		
}

class Node{
	public Node(int x,int y) {
		this.x = x;
		this.y = y;
	}
	int x,y;
}

猜你喜欢

转载自blog.csdn.net/weixin_38902950/article/details/81172170