chenxi16

导航

数据结构1 07-图5 Saving James Bond - Hard Version

 

  1 #include<stdio.h>
  2 #include<math.h>
  3 #define MAXN 102
  4 #define INF 1000000000
  5 struct pos{
  6     int x,y;
  7 };
  8 struct edge{
  9     int v1,v2;
 10 };
 11 struct pos p[MAXN];
 12 int g[MAXN][MAXN],vis[MAXN],n,d,flag,dist[MAXN],w[MAXN][MAXN],weight[MAXN],path[MAXN];
 13 void init();
 14 void read();
 15 void creatg();
 16 void dijkstra(int x);
 17 double dis(struct pos p1,struct pos p2);
 18 int main(){
 19     int i;
 20     flag=0;
 21     read();
 22     creatg();
 23     dijkstra(0);
 24     if(dist[n+1]!=INF){
 25         printf("%d\n",dist[n+1]);
 26         int stack[MAXN],top=0,pre;
 27         pre = path[n+1];
 28         while(pre!=0){
 29             stack[top++] = pre;
 30             pre = path[pre];
 31         }
 32         for(i=top-1;i>=0;i--){
 33             pre = stack[i];
 34             if(pre!=0&&pre!=n+1)
 35                 printf("%d %d\n",p[pre].x,p[pre].y);
 36         }
 37         
 38         
 39     }
 40     else printf("0\n");
 41     return 0;
 42 }
 43 void init(){
 44     int i,j;
 45     
 46     for(i=0;i<=n+1;i++)
 47        for(j=0;j<=n+1;j++)
 48           {
 49               g[i][j] = INF;
 50               w[i][j] = INF;
 51           }
 52 }
 53 void read(){
 54     int i,j;
 55     scanf("%d %d",&n,&d);
 56     init();
 57     for(i=1;i<=n;i++)
 58        scanf("%d %d",&p[i].x,&p[i].y);
 59 }
 60 double dis(struct pos p1,struct pos p2){
 61     double temp;
 62     temp = pow((p1.x-p2.x),2) + pow((p1.y-p2.y),2);
 63     return sqrt(temp);
 64 }
 65 void creatg(){
 66     int i,j;
 67     double t;
 68     struct pos o;
 69     o.x = 0;
 70     o.y = 0;
 71     if((50-15.2)<=d){
 72         g[0][n+1] = 1;
 73         g[n+1][0] = 1;
 74         w[0][n+1] = ceil((50-15.2));
 75         w[n+1][0] = w[0][n+1];
 76     }
 77     for(i=1;i<=n;i++){
 78         t = (dis(o,p[i])-15/2.0);
 79         
 80         if(t<=d){
 81             g[0][i] = 1;
 82             g[i][0] = 1;
 83             w[0][i] =ceil(t);
 84             w[i][0] =ceil(t);
 85         }
 86         
 87         
 88         if((50-abs(p[i].x))<=d||((50-abs(p[i].y))<=d)){
 89             g[i][n+1] = 1;
 90             g[n+1][i] = 1;
 91             w[i][n+1] = 1;
 92             w[n+1][i] = 1;
 93             
 94         }    
 95     }
 96     for(i=1;i<=n;i++){
 97         for(j=1;j<=n;j++){
 98             if(i!=j && dis(p[i],p[j])<=d){
 99                 g[i][j] = 1;
100                 g[j][i] = 1;
101                 w[i][j] = 1;
102                 w[j][i] = 1;
103             }
104         }
105     }
106 } 
107 void dijkstra(int x){
108     int i,mdist,mn;
109     for(i=0;i<MAXN;i++) { dist[i] = INF; weight[i] = INF;}
110     for(i=0;i<=n+1;i++) vis[i]=0;
111     dist[x] = 0;
112     weight[x] =0;
113     while(1){
114         mdist = INF;
115         mn=-1;
116         for(i=0;i<=n+1;i++)
117            if(mdist > dist[i] && vis[i]==0 ){
118                   mn = i;
119                   mdist = dist[i];
120            }
121         vis[mn] = 1;
122         if(mn==-1) break;
123         for(i=0;i<=n+1;i++){
124             if(vis[i]==0){
125                 if(g[mn][i]!=INF){
126                     if(g[mn][i]+dist[mn]<dist[i]){
127                         dist[i] = g[mn][i] + dist[mn];
128                         weight[i] = w[mn][i] +weight[mn];    
129                         path[i] = mn;
130                     }
131                     else if(g[mn][i]+dist[mn]==dist[i]){
132                         if(w[mn][i] + weight[mn] < weight[i]){
133                             weight[i] = w[mn][i] +weight[mn];    
134                             path[i] = mn;
135                         }
136                     }
137                 }
138             }
139         }
140         
141     }
142     
143     
144 }
View Code

 

posted on 2020-04-05 11:48  chenxi16  阅读(141)  评论(0编辑  收藏  举报