C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng)
本文實(shí)例為大家分享了C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
首先是配置文件
//文件名'MGraph.h'
//用途:創(chuàng)建鄰接矩陣
#include<iostream>
#include<stdlib.h>
using namespace std;
/*
*author:xcy?
*date:2019.6.1?
*/
#define MaxInt 32767 //表示極大值
#define MaxNum 100 ?//表示最大頂點(diǎn)數(shù)
typedef int status;
typedef string VerTexType; ?//頂點(diǎn)的數(shù)據(jù)類型
typedef int ArcType; ?//邊的權(quán)值為整型
typedef struct
{
? ? VerTexType vexs[MaxNum]; ? //頂點(diǎn)表
? ? ArcType arcs[MaxNum][MaxNum]; ? //鄰接矩陣
? ? int vexnum,arcnum;//當(dāng)前的點(diǎn)和邊數(shù)
? ? char name[MaxNum];
}AMGraph;
status CreateMap(AMGraph &G)//地圖的創(chuàng)建?
{
? ? G.vexnum=10;?
? ? G.arcnum=13;
? ? G.vexs[0]="北門";
? ? G.vexs[1]="下沉廣場(chǎng)";
? ? G.vexs[2]="青年公寓";
? ? G.vexs[3]="齊賢廣場(chǎng)";
? ? G.vexs[4]="15教";
? ? G.vexs[5]="菜鳥(niǎo)驛站";
? ? G.vexs[6]="匯森樓";
? ? G.vexs[7]="圖書館";
? ? G.vexs[8]="體育館";
? ? G.vexs[9]="南苑餐廳";
? ??
? ? for(int i=0;i<MaxNum;i++)//初始化所有頂點(diǎn)之間距離?
? ? {
? ? ? ? for(int j=0;j<MaxNum;j++)
? ? ? ? {
? ? ? ? ? ? G.arcs[i][j] = MaxInt;
? ? ? ? }
? ? }
? ? //各頂點(diǎn)之間距離
? ? G.arcs[0][1] = G.arcs[1][0] = 300;?? ?
?? ?G.arcs[0][2] = G.arcs[2][0] = 600;?? ?
?? ?G.arcs[1][2] = G.arcs[2][1] = 200;?? ?
?? ?G.arcs[2][3] = G.arcs[3][2] = 600;?? ?
?? ?G.arcs[2][6] = G.arcs[6][2] = 1000;
?? ?G.arcs[3][4] = G.arcs[4][3] = 100;?? ?
?? ?G.arcs[3][6] = G.arcs[6][3] = 800;?? ?
?? ?G.arcs[4][5] = G.arcs[5][4] = 100;?? ?
?? ?G.arcs[4][8] = G.arcs[8][4] = 400;
?? ?G.arcs[5][9] = G.arcs[9][5] = 700;
?? ?G.arcs[6][7] = G.arcs[7][6] = 100;
?? ?G.arcs[7][8] = G.arcs[8][7] = 100;
?? ?G.arcs[8][9] = G.arcs[9][8] = 300;
}具體方法實(shí)現(xiàn)
#include<iostream>
#include<stdlib.h>
#include"MGraph.h"?
using namespace std;
/*
*author:xcy?
*date:2019.6.12
*change:使用弗洛依德算法?
*/
int shortPath[MaxNum][MaxNum];//最短路徑長(zhǎng)度?
int Path[MaxNum][MaxNum];//保存下一個(gè)節(jié)點(diǎn)?
void ShortestPath_Floyd(AMGraph G)//弗洛依德算法
{
? ? int i,j,k;
? ? for(i=0;i<G.vexnum;i++)//循環(huán)遍歷所有頂點(diǎn)(橫列)?
? ? {
? ? ? ? for(j=0;j<G.vexnum;j++)//循環(huán)遍歷所有頂點(diǎn)(豎列)
? ? ? ? {
? ? ? ? ? ? shortPath[i][j]=G.arcs[i][j];//保存權(quán)值?
? ? ? ? ? ? if(shortPath[i][j]<MaxInt&&i!=j)?
? ? ? ? ? ? ?? ?Path[i][j]=j;//保存下一個(gè)頂點(diǎn)下標(biāo)?
? ? ? ? ? ? else Path[i][j]=-1;
? ? ? ? }
? ? }
? ? //算法核心語(yǔ)句?
? ? for(k=0;k<G.vexnum;k++)//遍歷所有點(diǎn)(作為中點(diǎn))?
? ? {
? ? ? ? for(i=0;i<G.vexnum;i++)//遍歷行?
? ? ? ? {
? ? ? ? ? ? for(j=0;j<G.vexnum;j++)//遍歷列?
? ? ? ? ? ? {
? ? ? ? ? ? ?? ?//松弛操作 如果經(jīng)歷k點(diǎn)距離小于兩點(diǎn)之間距離,選擇經(jīng)過(guò)k點(diǎn)的路線?
? ? ? ? ? ? ? ? if(shortPath[i][k]+shortPath[k][j]<shortPath[i][j])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? shortPath[i][j]=shortPath[i][k]+shortPath[k][j];
? ? ? ? ? ? ? ? ? ? Path[i][j]=Path[i][k];//更新操作?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
void math()//次級(jí)界面
{
? ? int a,b,i,j,k,m,n;
? ? AMGraph G;
? ? CreateMap(G);
? ? ShortestPath_Floyd(G);?? ?//弗洛依德算法
? ? cout<<"1.北門 2.下沉廣場(chǎng) 3.青年公寓 4.齊賢廣場(chǎng) 5.15教"<<endl;
? ? cout<<"6.菜鳥(niǎo)驛站 7.匯森樓 8.圖書館 9.體育館 10.南苑餐廳"<<endl;
? ? cout<<"輸入起點(diǎn)的序號(hào)"<<endl;
? ? cin>>a;
? ? cout<<"輸入終點(diǎn)的序號(hào)"<<endl;
? ? cin>>b;
? ? m=a-1;
? ? n=b-1;
? ? cout<<"最短路徑:"<<shortPath[m][n]<<"米"<<endl;
? ? cout<<"路徑為:"<<G.vexs[m];
? ? k=Path[m][n];
? ? while(k!=n)
? ? {
? ? ? ? cout<<" -> "<<G.vexs[k];
? ? ? ? k=Path[k][n];
? ? }
? ?cout<<" -> "<<G.vexs[n]<<endl;
}
void scence()//顯示地圖?
{
? ? cout<<"\t\t--------------------------------------------------------"<<endl;
? ? cout<<"\t\t| ? ? ? 北門 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |"<<endl;?
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|"<<endl;
? ? cout<<"\t\t| ?下沉 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|"<<endl;
? ? cout<<"\t\t| ?廣場(chǎng) ? ? ? ? ? 青年 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? 公寓 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 菜鳥(niǎo) ? ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ?15教 ? ? 驛站 ? ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ?齊賢廣場(chǎng) ? ? ? ? ? ? ? ? ? ? ? ? ?|"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 南苑 ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 餐廳 ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? 匯森樓 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t| ? ? ? ? ? ? ? ? ? ? ? ? 圖書館 ? ?體育館 ? ? ? ? ? ? |"<<endl;
? ? cout<<"\t\t--------------------------------------------------------"<<endl;
}
void Gui()//主界面
{
? ? char a;
? ? cout<<"\t\t\t┌--------------------------------┐"<<endl;
? ? cout<<"\t\t\t│ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?│"<<endl;
? ? cout<<"\t\t\t│ ? 歡迎使用南工校園導(dǎo)航系統(tǒng) ? ? │"<<endl;
? ? cout<<"\t\t\t│ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?│"<<endl;
? ? cout<<"\t\t\t│--------------------------------│"<<endl;
? ? cout<<"\t\t\t│ ? ? ?1. 查看校園全貌 ? ? ? ? ? │"<<endl;
? ? cout<<"\t\t\t│ ? ? ?2. 計(jì)算最短路徑 ? ? ? ? ? │"<<endl;
? ? cout<<"\t\t\t│ ? ? ?3. 退出導(dǎo)航系統(tǒng) ? ? ? ? ? │"<<endl;
? ? cout<<"\t\t\t└--------------------------------┘"<<endl;
? ? while(true){
? ? cout<<"\t\t\t輸入要操作的序號(hào)(1-3):";
? ? ?? ?cin>>a;
? ? ?? ?(int)a;
? ? ?? ?if(a>'0'&&a<='3')
?? ??? ??? ?switch(a)
?? ? ? ??? ?{
?? ? ? ? ??? ??? ?case '1': scence(); break;
?? ? ? ? ??? ??? ?case '2': math(); break;
?? ? ? ? ??? ??? ?case '3': cout<<"\t\t\t感謝您的使用!";exit(0);break;
?? ? ??? ??? ?}
? ?? ??? ?else cout<<"\t\t\t請(qǐng)輸入1-3!!"<<endl;?
?? ?} ??
}
int main()
{
? ? Gui();
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)經(jīng)典掃雷小游戲完整代碼(遞歸展開(kāi)?+?選擇標(biāo)記)
這篇文章主要介紹了C語(yǔ)言小項(xiàng)目之掃雷游戲帶遞歸展開(kāi)?+?選擇標(biāo)記效果,本代碼中,我們用字符?!?來(lái)標(biāo)識(shí)雷,文中附有完整代碼,需要的朋友可以參考下2022-05-05
C語(yǔ)言字符串左旋的兩種實(shí)現(xiàn)方法
匯編語(yǔ)言中有一種移位指令叫做循環(huán)左移(ROL),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言字符串左旋的兩種實(shí)現(xiàn)方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
C++實(shí)現(xiàn)LeetCode(59.螺旋矩陣之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(59.螺旋矩陣之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
基于epoll的多線程網(wǎng)絡(luò)服務(wù)程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了基于epoll的多線程網(wǎng)絡(luò)服務(wù)程序設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題
今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
用C語(yǔ)言模仿Python函數(shù)的一種簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了用C語(yǔ)言模仿Python函數(shù)的一種簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2017-05-05

