Saving James Bond - Hard Version

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
  1 #include<iostream>
  2 #include<vector>
  3 #include<math.h>
  4 #include<queue>
  5 #include<stack>
  6 using namespace std;
  7 #define Maxnodenum 101
  8 #define nolimitmax 100000
  9 int flag=0;//为了标志第一次拓展外层
 10 vector<double> dist(Maxnodenum,nolimitmax);//为了记住跳到每个点的步数
 11 vector<int> path(Maxnodenum,-1);//为了记录路径
 12 struct vertex{
 13 int x;
 14 int y;
 15 };
 16 struct graph{
 17 int Nv;
 18 int jump;
 19 vertex G[Maxnodenum];
 20 };
 21 using Graph=graph*;
 22 Graph BuildGraph(){
 23 int v,x,y;
 24 Graph gra=new graph();
 25 cin>>gra->Nv>>gra->jump;
 26 gra->G[0].x=gra->G[0].y=0;
 27     for(v=1;v<=gra->Nv;v++)
 28     {
 29     cin>>gra->G[v].x>>gra->G[v].y;
 30 }
 31 return gra;
 32 }
 33 double distance(vertex n1,vertex n2){
 34 return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));
 35 }
 36 int saved(Graph gra,int v){
 37 if(gra->G[v].x>=50-gra->jump||gra->G[v].x<=-50+gra->jump||gra->G[v].y>=50-gra->jump||gra->G[v].y<=-50+gra->jump)
 38 {while(path[v]!=-1){ 
 39     
 40     if(path[v]==0) { return 1;}
 41     v=path[v];        
 42 } 
 43  }  
 44     return 0;
 45 }
 46 bool point(Graph gra,int v){
 47 int x=gra->G[v].x; int y=gra->G[v].y;
 48 if(x*x+y*y<=7.5*7.5||x>=50||x<=-50||y>=50||y<=-50)
 49 {return false;}
 50 else {return true;
 51 }
 52 }
 53 void save(Graph gra){
 54 if(gra->jump>=50){
 55 cout<<1; return;
 56 }
 57     dist[0]=0; int v=0,v1;
 58     queue<int> q;
 59     q.push(v);
 60     while(!q.empty()){
 61     v=q.front(); q.pop();
 62 if(flag==0){
 63 for(v1=1;v1<=gra->Nv;v1++){
 64     if(point(gra,v1)&&distance(gra->G[0],gra->G[v1])-7.5<=gra->jump&&path[v1]==-1)
 65     { q.push(v1); dist[v1]=dist[v]+1; path[v1]=v;}
 66 }}
 67     flag++;
 68     if(flag!=0){
 69     for(v1=1;v1<=gra->Nv;v1++)
 70     if(point(gra,v1)&&distance(gra->G[v],gra->G[v1])<=gra->jump&&path[v1]==-1)
 71 { q.push(v1); dist[v1]=dist[v]+1; path[v1]=v;}}
 72 }
 73     int minstep=10000,laststep,v2;
 74     stack<int> s;
 75     for(v=1;v<=gra->Nv;v++){
 76     if(saved(gra,v)){
 77     if(dist[v]<minstep){
 78     minstep=dist[v];
 79     laststep=v;
 80 }else if(dist[v]==minstep){
 81 v2=v;
 82 while(path[v2]!=0)
 83 v2=path[v2];
 84     v1=laststep;
 85 while(path[v1]!=0)
 86     v1=path[v1];
 87 laststep=distance(gra->G[0],gra->G[v2])<distance(gra->G[0],gra->G[v1])?v:laststep;
 88 }}}
 89 if(minstep!=10000){
 90 v=laststep; minstep++;
 91 cout<<minstep<<endl;
 92 while(path[v]!=-1){
 93 s.push(v);
 94 v=path[v];
 95 }
 96 while(!s.empty()){
 97 v=s.top();
 98 cout<<gra->G[v].x<<" "<<gra->G[v].y<<endl;
 99 s.pop();
100 }
101 }else 
102 cout<<0<<endl;
103 }
104 int main(){
105 Graph gra=BuildGraph();
106 save(gra);
107 return 0;
108 }
View Code

 

 

 

posted @ 2017-12-18 09:35  A-Little-Nut  阅读(300)  评论(0编辑  收藏  举报