题目:
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 12Sample Output 1:
YesSample Input 2:
4 13 -12 12 12 12 -12 -12 12 -12Sample Output 2:
No
在这里我们使用邻接表或者使用邻接图,我们使用的是Vector和堆栈来表示图,这样显示的更加方便,在这里不仅可以显示判断Yes或No,我们还可以显示出其需要踩得路径
1 #include<iostream> 2 #include<vector> 3 #include<math.h> 4 #include<stack> 5 using namespace std; 6 #define Radius 7.5 7 #define Minsize 42.5 8 9 10 double croconum,jumpdis; 11 12 struct Point{ 13 double x; 14 double y; 15 bool visited; 16 }; 17 Point O; 18 vector<Point>S;int flag=0;stack<int> path; 19 double Distance(Point a,Point b) 20 { 21 return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); 22 } 23 24 void SearchPath(int p) 25 { flag=0; 26 S[p].visited=true; 27 int i; 28 if((50-abs(S[p].x)<jumpdis) ||((50-abs(S[p].y)<jumpdis))) 29 { 30 cout<<"Yes"<<endl; 31 flag=1; 32 // exit(0); 33 return; 34 } 35 36 for(i=0;i<croconum;i++) 37 { 38 if(!S[i].visited &&(Distance(S[p],S[i])<=jumpdis)) 39 { 40 SearchPath(i); 41 if(flag==1) 42 { 43 path.push(i); 44 break; 45 } 46 } 47 } 48 49 } 50 51 int main() 52 { 53 54 cin>>croconum>>jumpdis; 55 Point p;vector<int>Firstjump;int i,j; 56 O.x=0;O.y=0;//O表示源点 57 for( i=0;i<croconum;i++) 58 { 59 cin>>p.x>>p.y ; 60 p.visited=false; 61 S.push_back(p); 62 } 63 64 //在其开始时候必须要选择一个在合适范围内的第一个点() 65 if(jumpdis>=Minsize) 66 { 67 cout<<"Yes"<<endl; 68 return 0; 69 } 70 71 for( i=0;i<S.size();i++) 72 { 73 if(Distance(S[i],O)<(jumpdis+Radius)) 74 { 75 Firstjump.push_back(i);//在第一步跳的时候选择在能跳的范围之内的点 76 77 } 78 } 79 80 81 82 if(Firstjump.empty()) 83 { 84 cout<<"No"<<endl; 85 return 0; 86 } 87 88 //从所跳的点中寻找路径,并且可以从第一跳中各个依次遍历,找到其下一级(范围之外)的点 89 //可以从第一跳选择不同的点依次遍历,直到找到完美路径为止 90 91 92 for(i=0;i<Firstjump.size();i++) 93 { 94 if(!S[Firstjump[i]].visited) 95 SearchPath(Firstjump[i]); 96 if(flag==1) 97 { 98 path.push(Firstjump[i]); 99 cout<<"{"; 100 while(!path.empty()) 101 { 102 cout<<path.top()<<" "; 103 path.pop(); 104 } 105 cout<<"}"<<endl; 106 exit(0); 107 } 108 } 109 //cout<<"No"<<endl; 110 if(flag==0) 111 { 112 cout<<"No"<<endl; 113 } 114 return 0; 115 116 }