# include <stdio.h> # include <string.h> # include <stdlib.h> //校園導航系統 # define Max 100 # define INF 10000 typedef struct place { char name[20];//名字 int index;//下標 char intro[30];//簡介 }PLA;//定義位置 typedef struct { PLA place[Max]; float edge[Max][Max]; int vertexnum;//點的數目 int edgenum;//邊數目 }Graph;//定義一個圖 void Init_Graph(Graph *G);//初始化一個圖 void show_menu(Graph * G);//輸出菜單 void shortestdistance(Graph *G);//求最短距離 void display(Graph *G,int start,int end);//展示路徑 int path[Max][Max]={0}; int main(void) { Graph G; Init_Graph(&G); show_menu(&G); return 0; } void Init_Graph(Graph *G) { int i,j; G->vertexnum=17; G->edgenum=20; ///////最初初始化一下圖 for(i=0;i<G->vertexnum;i++) { for(j=0;j<G->vertexnum;j++) { if(i==j) { G->edge[i][j]=0;//初始化如果自己和自己就為0 } else { G->edge[i][j]=INF;//否則初始化為無線大 } } } ////////////////////具體輸入圖 G->place[0].index=1; strcpy(G->place[0].name,"科學樓"); strcpy(G->place[0].intro,"特別大氣漂亮,是學校的門面\n"); G->place[1].index=2; strcpy(G->place[1].name ,"建國大飯店"); strcpy(G->place[1].intro,"接待外賓的去所\n"); G->place[2].index=3; strcpy(G->place[2].name,"人文樓"); strcpy(G->place[2].intro,"里面有外語、建規等多個學院\n"); G->place[3].index=4; strcpy(G->place[3].name,"經管樓"); strcpy(G->place[3].intro,"經管學院,漂亮妹子多\n"); G->place[4].index=5; strcpy(G->place[4].name,"奧運場館"); strcpy(G->place[4].intro,"工大羽毛球館,林丹這里奪的冠!\n"); G->place[5].index=6; strcpy(G->place[5].name,"美食園"); strcpy(G->place[5].intro,"比較好吃花樣多,除了貴一點啥都好!\n"); G->place[6].index=7; strcpy(G->place[6].name,"奧運餐廳"); strcpy(G->place[6].intro,"好吃不貴,幾乎一律六塊,你值得擁有!\n"); G->place[7].index=8; strcpy(G->place[7].name,"三教"); strcpy(G->place[7].intro,"上課的地方,上自習得好地方!\n"); G->place[8].index=9; strcpy(G->place[8].name,"四教"); strcpy(G->place[8].intro,"都柏林學院\n"); G->place[9].index=10; strcpy(G->place[9].name,"舊圖書館"); strcpy(G->place[9].intro,"學習氛圍很好,不過工科書多,缺少文化氛圍\n"); G->place[10].index=11; strcpy(G->place[10].name,"一教"); strcpy(G->place[10].intro,"桌椅和高中一樣,暖氣很暖和,很舒服\n"); G->place[11].index=12; strcpy(G->place[11].name,"二教"); strcpy(G->place[11].intro,"和一教差不多\n"); G->place[12].index=13; strcpy(G->place[12].name,"天天餐廳"); strcpy(G->place[12].intro,"分兩層,一層實惠,二層高檔\n"); G->place[13].index=14; strcpy(G->place[13].name,"四號宿舍樓"); strcpy(G->place[13].intro,"我住在這里,感覺環境欠改善\n"); G->place[14].index=15; strcpy(G->place[14].name,"北門"); strcpy(G->place[14].intro,"有烤冷面小販出沒\n"); G->place[15].index=16; strcpy(G->place[15].name,"校醫院"); strcpy(G->place[15].intro,"小病報銷比例高,大病看不了\n"); G->place[16].index=17; strcpy(G->place[16].name,"浴室"); strcpy(G->place[16].intro,"非常方便,水溫合適\n"); ///////////////////////////////////////// //具體距離 G->edge[0][1]=20; G->edge[1][0]=20; G->edge[0][2]=30; G->edge[2][0]=30; G->edge[1][3]=50; G->edge[3][1]=50; G->edge[2][4]=40; G->edge[4][2]=40; G->edge[3][4]=70; G->edge[4][3]=70; G->edge[3][5]=30; G->edge[5][3]=30; G->edge[5][6]=10; G->edge[6][5]=10; G->edge[6][4]=30; G->edge[4][6]=30; G->edge[5][7]=60; G->edge[7][5]=60; G->edge[7][8]=100; G->edge[8][7]=100; G->edge[8][6]=150; G->edge[6][8]=150; G->edge[7][11]=90; G->edge[11][7]=90; G->edge[11][9]=50; G->edge[9][11]=50; G->edge[9][8]=250; G->edge[8][9]=250; G->edge[11][10]=5; G->edge[10][11]=5; G->edge[10][12]=20; G->edge[12][10]=20; G->edge[12][13]=10; G->edge[13][12]=10; G->edge[13][16]=5; G->edge[16][13]=5; G->edge[15][16]=10; G->edge[16][15]=10; G->edge[14][15]=2; G->edge[15][14]=2; } void show_menu(Graph *G) { int choice; int start; int end; int i; int returnmenu; int place; float minroad; int flag=0; do { system("cls"); printf("----------歡迎來到某大學校園導航系統---------\n"); printf("---------- 以下為簡易地圖 ----------\n"); printf(" 15.北門 16.校醫院 \n"); printf(" 17.浴室 \n"); printf(" \n"); printf(" 14.四號宿舍樓 \n"); printf(" 11.一教 13.天天餐廳 \n"); printf(" 12.二教 10.舊圖書館 \n"); printf(" \n"); printf(" 8.三教 9.四教 \n"); printf(" \n"); printf(" 6.美食園 7.奧運餐廳 \n"); printf(" \n"); printf(" 4.經管樓 5.奧運場館 \n"); printf(" \n"); printf("2.建國飯店 3.人文樓 \n"); printf(" 1.科學樓 \n"); printf(" 北 \n"); printf(" | \n"); printf(" 西---------東\n"); printf(" | \n"); printf(" 南 \n"); printf("請輸入您所選擇的功能序號:\n"); printf("1.查詢最短路徑\n"); printf("2.查詢景點信息\n"); printf("3.退出\n"); scanf("%d",&choice); if(choice==1) { printf("請輸入起點以及終點序號:\n"); scanf("%d%d",&start,&end); if(start<0||start>17||end<0||end>17) { printf("輸入有誤!\n"); exit(-1); } shortestdistance(G); minroad=G->edge[start-1][end-1]; display(G,start,end); printf("最小路徑為:%.2f M\n",minroad); } else if(choice==2) { printf("請輸入您想查詢位置的序號\n"); scanf("%d",&place); for(i=0;i<G->vertexnum;i++) { if(place==i+1) { printf("該地點為:\n"); puts(G->place[i].name); printf("簡介為:\n"); puts(G->place[i].intro); } } } else if(choice==3) { exit(-1); } else { printf("輸入錯誤!\n"); } printf("請輸入是否返回主菜單(1就是,2就是不是):\n"); scanf("%d",&returnmenu); if(returnmenu==1) { flag=0; } else if(returnmenu==2) { flag=1; } } while(flag==0); } void shortestdistance(Graph *G) { int k,v,w; //k為中間點 // v為起點 //w為終點 for(k=0;k<G->vertexnum;k++) { for(v=0;v<G->vertexnum;v++) { for(w=0;w<G->vertexnum;w++) { if(G->edge[v][w]>(G->edge[v][k]+G->edge[k][w])) { G->edge[v][w]=G->edge[v][k]+G->edge[k][w]; path[v][w]=k;//v->w要經過 k path[w][v]=k;//w->v要經過 k } } } } } void display(Graph *G,int start,int end) { int i=start-1; int j=end-1; printf("您要查詢的兩景點間最短路徑是: "); if(G->edge[i][j]!=INF) { if(i<j) { printf("%s",G->place[end-1].name); while(path[i][j]!=0) {/*?把i到j的路徑上所有經過的景點按逆序打印出來*/ printf("<--"); printf("%s",G->place[path[i][j]].name); if(i<j) j=path[i][j]; else i=path[j][i]; } printf("<--"); printf("%s",G->place[start-1].name); printf(" "); } else { printf("%s",G->place[start-1].name); while(path[i][j]!=0) {/*?把i到j的路徑上所有經過的景點按順序打印出來*/ printf("-->"); printf("%s",G->place[path[i][j]].name); if(i<j) j=path[i][j]; else i=path[j][i]; } printf("-->"); printf("%s",G->place[end-1].name); printf(" "); } } else printf("輸入錯誤!不存在此路!\n"); printf(" "); }