07-图5 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

#include<iostream> #include<queue> #include<stack> #include<vector> #include<cmath> #include<algorithm> using namespace std; const int maxn=105; int N,D,points[maxn][2]={0},safe[maxn]={0}; vector<int>v[maxn]; int visit[maxn]={0}; int firstJump(int i){ int d = pow(points[i][0],2) + pow(points[i][1],2); if( d <= pow(D+7.5,2)) return d; return 0; } int cmp(int a,int b){ return firstJump(a) < firstJump(b); } int Eascape(int x,int y){ if(50+x<=D || 50-y<=D || 50-x<=D || 50-y<=D) return 1; return 0; } void choosePath(){ queue<int>q; q.push(0); visit[0] = 1; int t,leftNum = 1,curNum = 0,last = 0,cnt = 1,fronts[maxn] = {0}; while(!q.empty()){ t=q.front(); q.pop(); leftNum--; int i = 0; for(;i<v[t].size();i++){ if(!visit[v[t][i]]){ visit[v[t][i]] = 1; q.push(v[t][i]); fronts[v[t][i]] = t; curNum++; } if(safe[v[t][i]]){ last = v[t][i]; break; } } if(leftNum==0){ leftNum = curNum; curNum = 0; } if(i<v[t].size()) break; } stack<int>s; s.push(last); for(int i=last;fronts[i];i=fronts[i]){ s.push(fronts[i]); } cnt += s.size(); cout<<cnt<<endl; while(s.size()){ t = s.top(); s.pop(); cout<<points[t][0]<<' '<<points[t][1]<<endl; } } int main(){ int x,y,firstNum = 0,mark=0,firstChoices[maxn] = {0}; cin>>N>>D; if(D>42.5) cout<<1<<endl; else{ int i = 1,Ncopy = N; while(Ncopy--){ cin>>x>>y; if(pow(x,2) + pow(y,2) < pow(7.5,2) || x<-50 || x>50 || y<-50 || y>50) continue; points[i][0] = x; points[i][1] = y; if(firstJump(i)) firstChoices[firstNum++] = i; if(Eascape(x,y)) safe[i]=1; for(int j=1;j<=i;j++){ if(pow(x-points[j][0],2) + pow(y-points[j][1],2) <= pow(D,2)){ v[i].push_back(j); v[j].push_back(i); } } i++; } sort(firstChoices,firstChoices+firstNum,cmp); for(int i=0;i<N;i++){ if(i<firstNum) v[0].push_back(firstChoices[i]); if(safe[i]) mark = 1; } if(v[0].size()<1 || !mark) cout<<0<<endl; else choosePath(); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-1900269.html