ZJUmooc 07-图5 Saving James Bond - Hard Version

题目在这儿看

坑点略多

先把比较丑的代码扔上来吧

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<math.h>
  5 #include<algorithm> 
  6 #define MAX_VERTEX_NUM 120
  7 #define INF 200
  8 typedef struct node{
  9     int vexnum;
 10     bool map[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
 11 }Graph;
 12 typedef struct Node{
 13     int x,y;
 14 }stonedata;
 15 double Distance(int u,int v,stonedata loc[]){
 16     return sqrt((loc[u].x-loc[v].x)*(loc[u].x-loc[v].x)+(loc[u].y-loc[v].y)*(loc[u].y-loc[v].y));
 17 }
 18 void Input(Graph* G,stonedata loc[],int maxdis){
 19     for (int i=1;i<=G->vexnum;i++){//输入数据,初始化1.0 
 20         scanf("%d%d",&loc[i].x,&loc[i].y);
 21         for (int j=0;j<=G->vexnum+1;j++){
 22             G->map[i][j]=0;
 23             G->map[j][i]=0;
 24         }
 25     }
 26     for (int i=1;i<=G->vexnum;i++){//初始化2.0 
 27         //与起点接通
 28         if (Distance(0,i,loc)-7.5<=(double)maxdis){
 29             G->map[0][i]=1;
 30             G->map[i][0]=1;
 31         } 
 32         //鳄鱼之间接通 
 33         for (int j=1;j<=G->vexnum;j++){
 34             if (Distance(i,j,loc)<=(double)maxdis){
 35                 G->map[i][j]=1;
 36                 G->map[j][i]=1;
 37             }
 38         } 
 39         //与岸边接通  
 40         if (abs(50-loc[i].x)<=maxdis||abs(50-loc[i].y)<=maxdis||
 41             abs(loc[i].x+50)<=maxdis||abs(loc[i].y+50)<=maxdis){
 42                 G->map[i][G->vexnum+1]=1;
 43                 G->map[G->vexnum+1][i]=1;
 44             } 
 45     }
 46 }
 47 void Output(int parent[],int current,stonedata loc[]){
 48     int stack[MAX_VERTEX_NUM];
 49     int top=1;
 50     while (current!=0){
 51         stack[top++]=current;
 52         current=parent[current];
 53     }
 54     while (--top){
 55         printf("%d %d\n",loc[stack[top]].x,loc[stack[top]].y);
 56     }
 57 }
 58 void bfsSolve(Graph G,int st,int ed,stonedata loc[]){
 59     //宽搜的准备工作 
 60     int queue[MAX_VERTEX_NUM*3];
 61     int front,rear;
 62     front=0;rear=1;
 63     queue[front]=st;
 64     int ans[MAX_VERTEX_NUM]={0};//记录出发点到当前点的距离 
 65     int last_point[MAX_VERTEX_NUM]={0};
 66     int FinalAns=INF;
 67     double FinalDisToFirst=INF;
 68     int FinalPath;
 69     //宽搜 
 70     while (front<rear){
 71         int current=queue[front++];
 72         if (ans[current]==FinalAns){
 73             printf("%d\n",FinalAns);
 74             Output(last_point,FinalPath,loc);
 75             return;
 76         }
 77         if (G.map[current][ed]){
 78             FinalAns=ans[current]+1;
 79             int temp=last_point[current];
 80             while (last_point[temp]!=0) temp=last_point[temp];
 81             if (Distance(0,temp,loc)<FinalDisToFirst){
 82                 FinalPath=current;
 83                 FinalDisToFirst=Distance(0,temp,loc);
 84             }
 85         }
 86         for (int i=1;i<=G.vexnum;i++){
 87             if (G.map[i][current]&&!ans[i]){
 88                 ans[i]=ans[current]+1;
 89                 last_point[i]=current;
 90                 queue[rear++]=i;
 91             }
 92         }
 93     }
 94     puts("0");
 95     return;
 96 }
 97 
 98 int main(){
 99     Graph G;
100     stonedata loc[MAX_VERTEX_NUM];
101     int maxdis;
102     scanf("%d%d",&G.vexnum,&maxdis);
103     if ((double)maxdis>=42.5){//一步就可以直接跳出去 
104         printf("%d\n",1);
105         return 0; 
106     }
107     Input(&G,loc,maxdis);//读入并完全初始化好图,编号0是起点,编号G.vertex+1是终点 
108     bfsSolve(G,0,G.vexnum+1,loc);
109     return 0;
110 }
View Code

 

posted @ 2020-05-09 10:45  huangwher  阅读(188)  评论(0)    收藏  举报