1. 程式人生 > >06-圖2 Saving James Bond

06-圖2 Saving James Bond

題目來源:中國大學MOOC-陳越、何欽銘-資料結構-2018春
作者: 陳越
單位: 浙江大學
問題描述:
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

解答:一開始沒看解析感覺這題好怪,走了些彎路。後來在姥姥的指引下寫出來的,感謝。
思維不能僵化,不是必須構造出一個鄰接矩陣或者鄰接表才能DFS,即使資料結構是陣列,也可以根據條件進行DFS,這道題就是這樣,把一隻只鱷魚想成點,根據鱷魚與鱷魚間的距離作為進行DFS的條件,每次DFS,從未訪問的結點中選出距離達標的結點進行訪問,判斷是否到岸可以想成已鱷魚為中心的圓是否與岸相切。最後要注意題目中說的diameter是直徑,因為這個卡了兩個測試點。。
需要注意的就是如何DFS,題目要審清楚,訪問過的結點需要標記,回退時要清除標記

#include <iostream>
#include <cmath>
#include <algorithm> #include <queue> using namespace std; const int maxn=101; struct Crocodile { int x,y; }; Crocodile crocodiles[maxn]; int isVisited[maxn]; int N; float D; void input() { int x1,y1; for(int i=0;i<N;i++) { cin>>crocodiles[i].x>>crocodiles[i].y; } } bool
isSafe(int x,int y) { //判斷能否上岸 if((50-abs(x)<=D)||(50-abs(y)<=D)) return true; else return false; } bool DFS(int x,int y,int crocoNum) { bool returnValue=false; if(isSafe(x,y)) return true; for(int i=0;i<N;i++) { float d=(crocodiles[i].x-x)*(crocodiles[i].x-x)+(crocodiles[i].y-y)*(crocodiles[i].y-y); if(isVisited[i]==0&&d<=D*D) { isVisited[i]=1; returnValue=DFS(crocodiles[i].x,crocodiles[i].y,i); if(returnValue) break; isVisited[i]=0; } } return returnValue; } bool findWay() { fill(isVisited,isVisited+maxn,0); bool judge=false; if(D>=42.5) return true; for(int i=0;i<N;i++) { float d=crocodiles[i].x*crocodiles[i].x+crocodiles[i].y*crocodiles[i].y; if(d<=(D+7.5)*(D+7.5)&&isVisited[i]==0) { isVisited[i]=1; judge=DFS(crocodiles[i].x,crocodiles[i].y,i); } if(judge==true) break; } return judge; } int main() { cin>>N>>D; input(); if(findWay()) cout<<"Yes"; else cout<<"No"; return 0; }