06-图2 Saving James Bond - Easy Version (25 分)
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
几经折腾,过了.
P类,里面有些无用变量,没删
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using System.Net; using System.Text; using System.Xml; class P { public P(string s) { var ss = s.Split(' '); x = int.Parse(ss[0]); y = int.Parse(ss[1]); check = false; } public P parent; public int step = int.MaxValue; public bool check; public int x, y; public int 距离; } class T { static void print(List<int> list) { string msg = "{ "; foreach (var item in list) { msg += item + " "; } msg += "}"; Console.WriteLine(msg); } static void printlist(P[] list) { Console.WriteLine(list.Length + 1); foreach (var item in list) { string msg = "{ "; msg = item.x + " " + item.y; //msg += "}"; Console.WriteLine(msg); } } static void Main(string[] args) { P first = new P(Console.ReadLine()); List<P> list = new List<P>(); //把鳄鱼加入列表,岸上的不加入 for (int i = 0; i < first.x; i++) { var item = new P(Console.ReadLine()); if (Math.Abs(item.x) >= 50 || Math.Abs(item.y) >= 50) { } else if (Math.Sqrt(item.x * item.x + item.y * item.y) <= 7.5) { } else { list.Add(item); } } var fp = new P("0 0"); //是否一步跳出 if (first.y >= 50) { Console.WriteLine("Yes"); return; } //先获取第一跳的所有点, var firstList = new List<P>(); for (int i = 0; i < list.Count; i++) { //第一跳因为有个7.5半径的岛.所以加了7.5 if (圆内么(first.y+7.5, fp, list[i])) { firstList.Add(list[i]); list[i].距离 = list[i].x * list[i].x + list[i].y * list[i].y; list.RemoveAt(i); i--; } } //为第一跳排序 firstList.Sort((a, b) => a.距离 > b.距离 ? 1 : 0); foreach (var item in firstList) { 规划路径(first.y, item, list); } //没有路径,说明没能跳出去 if (paths.Count == 0) { Console.WriteLine("No"); return; } Console.WriteLine("Yes"); return; //找出最小跳次数 var min = int.MaxValue; foreach (var item in paths) { min = min < item.Length ? min : item.Length; } //找出第一个最小跳的,因为之前排序了,所以第一个就是 var isit = paths.Find(p => p.Length == min); printlist(isit); return; } static List<P[]> paths = new List<P[]>(); static List<P> path = new List<P>(); static bool 圆内么(double 距离, P p1, P p2) { var d = Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); if (d <= 距离) return true; else return false; } static bool 能否跳到岸边(int 距离, P p) { if (Math.Abs(p.x) + 距离 >= 50 || Math.Abs(p.y )+ 距离 >= 50 ) { return true; } return false; } private static void 规划路径(int 距离, P p1, List<P> list) { path.Add(p1); if (能否跳到岸边(距离, p1))//如果这个点之后就能跳出,则加入这条路 {//可以跳出则加上这条路 paths.Add(path.ToArray()); } else { //不能跳出,则看下一个点 for (int i = 0; i < list.Count; i++) { var item = list[i]; if (path.Contains(item)) {//防止重复添加同一个点 continue; } if (圆内么(距离, p1, item)) { //这个点在园内,则递归判断 规划路径(距离, item, list); } } } //我判断完了,把原来的路径样子还归还,把之前添加的点,给去掉. path.RemoveAt(path.Count - 1); } }