數據結構課程設計:4、校園導遊諮詢(圖的應用)

                                                  校園導遊諮詢(圖的應用)

[問題描述]

 

設計一個校園導遊程序,爲來訪的客人提供各種信息查詢服務。

(1)設計學校的校園平面圖,所含景點不少於10個,以圖中頂點表示校內各景點,存放景點名稱、代號、簡介等信息;以邊表示路徑,存放路徑長度等相關信息;

(2)提供基本信息的修改功能;

(3)爲來訪客人提供圖中任意景點相關信息的查詢;

(4)爲來訪客人提供景點的問路查詢,即已知一個景點,查詢到某景點之間的一條最短路徑及長度。

 

[設計思路]

 

  1. 結構使用鄰接表
  2. 最短路使用迪傑斯特拉算法
  3. 前驅數組存放具體路徑

[代碼及註釋]

#include<iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f                        /*無窮大*/
#define MAXSIZE 11                            /*最大景點數*/
int flag;                                     /*判斷程序是否退出*/
//結構
typedef struct{
    int num;                                  /*景點編號*/
    string name;                              /*景點名稱*/
    string information;                       /*景點簡介*/
    string others;                            /*景點備註*/
}area;                                        /*結點*/
int dis[MAXSIZE],vis[MAXSIZE],pre[MAXSIZE];   /*dis:最短路徑,vis:標記,pre:前驅*/
typedef struct{
    int to;                                   /*目的景點*/
    int len;                                  /*路線長度*/
}Edge;
vector <Edge> Graph[MAXSIZE];                 /*鄰接表儲存*/
//子函數
void Creat();                                 /*建立鄰接表*/
void Show();                                  /*打印平面圖*/
void QueryArea();                             /*查詢景點信息*/
void Resive();                                /*修改*/
void QueryRoad();                             /*尋找最短路*/
void menu();                                  /*操作彙總*/
//具體
area Area[MAXSIZE]={
        {0,"---","----------"},
        {1,"國教樓","國外合作辦學,學習氛圍良好"},
        {2,"北苑餐廳","食物營養美味,種類多"},
        {3,"體育館","器材豐富,鍛鍊身體"},
        {4,"北苑操場","場地廣闊,適合運動"},
        {5,"行政樓","行政工作場地"},
        {6,"圖書館","最棒的一座樓,適合學習"},
        {7,"三號組團樓","多個學院辦公彙集地"},
        {8,"四號樓","一樓自由機房"},
        {9,"南苑餐廳","便宜健康"},
        {10,"南苑操場","供生活在南苑的學子們活動的場所"}
};                                            /*初始化各景點信息,初始沒有備註*/
void Creat()
{
    Graph[1].push_back(Edge{2,500});
    Graph[2].push_back(Edge{1,500});

    Graph[2].push_back(Edge{3,1000});
    Graph[3].push_back(Edge{2,1000});

    Graph[2].push_back(Edge{4,900});
    Graph[4].push_back(Edge{2,900});

    Graph[3].push_back(Edge{5,600});
    Graph[5].push_back(Edge{3,600});

    Graph[3].push_back(Edge{4,50});
    Graph[4].push_back(Edge{3,50});

    Graph[4].push_back(Edge{6,1200});
    Graph[6].push_back(Edge{4,1200});

    Graph[5].push_back(Edge{6,150});
    Graph[6].push_back(Edge{5,150});

    Graph[6].push_back(Edge{8,750});
    Graph[8].push_back(Edge{6,750});

    Graph[4].push_back(Edge{7,1500});
    Graph[7].push_back(Edge{4,1500});

    Graph[6].push_back(Edge{7,600});
    Graph[7].push_back(Edge{6,600});

    Graph[7].push_back(Edge{9,1200});
    Graph[9].push_back(Edge{7,1200});

    Graph[8].push_back(Edge{9,500});
    Graph[9].push_back(Edge{8,500});

    Graph[8].push_back(Edge{10,1200});
    Graph[10].push_back(Edge{8,1200});

    Graph[9].push_back(Edge{10,900});
    Graph[10].push_back(Edge{9,900});

}                                             /*鄰接表的初始化*/
void Show()
{
    cout<<"             ------------------------------------------------------"<<endl;
    cout<<"            |                    中工平面圖                        |"<<endl;
    cout<<"             ------------------------------------------------------"<<endl;
    cout<<"            |                      北門                            |"<<endl;
    cout<<"            |                       ||                             |"<<endl;
    cout<<"            |                   [1]國教樓                          |"<<endl;
    cout<<"            |                       ||                             |"<<endl;
    cout<<"            |                       ||  500m                       |"<<endl;
    cout<<"            |                       ||                             |"<<endl;
    cout<<"            |                   [2]北苑餐廳                        |"<<endl;
    cout<<"            |                       ||                             |"<<endl;
    cout<<"            |                       ||                             |"<<endl;
    cout<<"            |                  1000m||                             |"<<endl;
    cout<<"            |                       || 900m                        |"<<endl;
    cout<<"            |                     // ||                            |"<<endl;
    cout<<"            |                    //   ||                           |"<<endl;
    cout<<"            |           [3]體育館-50m-[4]北苑操場---||             |"<<endl;
    cout<<"            |                   ||   ||             ||             |"<<endl;
    cout<<"            |               600m||   ||             || 1500m       |"<<endl;
    cout<<"            |                   ||   || 1200m       ||             |"<<endl;
    cout<<"            |              [5]行政樓 ||             ||             |"<<endl;
    cout<<"            |                   ||[6]圖書館  [7]三號組團樓         |"<<endl;
    cout<<"            |                  //150m||             ||             |"<<endl;
    cout<<"            |西門--------------------||             || 1200m       |"<<endl;
    cout<<"            |                        || 750m        ||             |"<<endl;
    cout<<"            |                        ||             ||--------東門 |"<<endl;
    cout<<"            |                       //    500m      ||             |"<<endl;
    cout<<"            |                  [8]四號樓-------[9]南苑餐廳         |"<<endl;
    cout<<"            |                       ||              ||             |"<<endl;
    cout<<"            |                       ||              ||             |"<<endl;
    cout<<"            |                       ||              //             |"<<endl;
    cout<<"            |                       || 1200m      -- 900m          |"<<endl;
    cout<<"            |                       ||-----[10]南苑操場            |"<<endl;
    cout<<"            |                                      ||              |"<<endl;
    cout<<"            |                                     南門             |"<<endl;
    cout<<"            |                                                      |"<<endl;
    cout<<"             ------------------------------------------------------"<<endl;
}
void QueryArea()
{
    Show();
    cout<<"輸入要查詢的景點編號(1~10):";
    int n;
    cin>>n;
    if(n<1||n>10)
    {
        cout<<"輸入的編號不合法!"<<endl;
        return;
    }
    cout<<"------------------------------------------------------"<<endl;
    cout<<"<<<<編號爲"<<n<<"的景點"<<endl;
    cout<<"|名稱: "<<Area[n].name<<endl;
    cout<<"|簡介:"<<Area[n].information<<endl;
    cout<<"|備註:"<<Area[n].others<<endl;
    cout<<"------------------------------------------------------"<<endl;
}
void Resive()
{
    Show();
    cout<<"輸入要修改的景點編號:";
    int n;
    cin>>n;
    if(n<1||n>10)
    {
        cout<<"輸入的編號不合法!"<<endl;
        return;
    }
    cout<<"                           ------------------------"<<endl;
    cout<<"                          |      景點修改系統      |"<<endl;
    cout<<"                          |------------------------|"<<endl;
    cout<<"                          |      1--景點簡介       |"<<endl;
    cout<<"                          |      2--修改備註       |"<<endl;
    cout<<"                          |      其他--無變化      |"<<endl;
    cout<<"                           ------------------------"<<endl;
    int op;
    cout<<"輸入你的選擇:";
    cin>>op;
    switch(op)
    {
        case 1:{
            string Information;
            cout<<"輸入即將修改的內容:";
            cin>>Information;
            Area[n].information=Information;
            cout<<"<<<<修改成功"<<endl;
            }
            break;
        case 2:{
            string Others;
            cout<<"輸入要修改的備註:";
            cin>>Others;
            Area[n].others=Others;
            cout<<"<<<<修改成功"<<endl;
            }
            break;
        default :cout<<"<<<<沒有任何改動!"<<endl;
    }
    cout<<"------------------------------"<<endl;
}
void QueryRoad()
{
    Show();
    Creat();
    cout<<"輸入起始點編號與目的地編號,空格隔開:";
    int s,e;
    cin>>s>>e;
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,0,sizeof(pre));
    dis[s]=0;                                 /*自身到自身距離置爲0*/
    int k=MAXSIZE-1;                          /*MAXSIZE-1次查找*/
    while(k--)
    {
        int MAX=inf,from;
        for(int i=1;i<MAXSIZE;++i)
            if(!vis[i]&&dis[i]<MAX)
            {
                MAX=dis[i];
                from=i;
            }
        vis[from]=1;
        for(int i=0;i<Graph[from].size();++i)
        {
            int to=Graph[from][i].to;
            int len=Graph[from][i].len;
            if(!vis[to]&&dis[to]>dis[from]+len)
            {
                dis[to]=dis[from]+len;
                pre[to]=from;               /*記錄前驅*/
            }
        }
    }
    cout<<Area[s].name<<"到"<<Area[e].name<<"的最短距離爲"<<dis[e]<<"米"<<endl;
    cout<<"<<<<具體路徑爲:"<<endl;
    int temp[MAXSIZE];
    k=1;
    temp[1]=e;
    while(pre[e])
    {
        temp[++k]=pre[e];
        e=pre[e];
    }
    cout<<Area[temp[k]].name;
    for(int i=k-1;i>=1;--i)
        cout<<"->"<<Area[temp[i]].name;
                                            /*打印具體路徑*/
    cout<<endl;
    cout<<"-----------------------------------"<<endl;
}
void menu()
{
    cout<<"                           ------------------------"<<endl;
    cout<<"                          |  歡迎來到中工導遊系統  |"<<endl;
    cout<<"                          |------------------------|"<<endl;
    cout<<"                          |     1--查看平面圖      |"<<endl;
    cout<<"                          |     2--修改景點信息    |"<<endl;
    cout<<"                          |     3--查看景點信息    |"<<endl;
    cout<<"                          |     4--查詢最短路徑    |"<<endl;
    cout<<"                          |     其他--退出         |"<<endl;
    cout<<"                           ------------------------"<<endl;
    int op;
    cout<<"輸入你的選擇:";
    cin>>op;
    switch(op)
    {
        case 1:Show();break;
        case 2:Resive();break;
        case 3:QueryArea();break;
        case 4:QueryRoad();break;
        default :flag=1;cout<<"<<<<歡迎下次再來!"<<endl;
    }
}
int main()
{
    while(1)
    {
        menu();
        if(flag)break;
        system("pause");
        system("cls");
    }
    return 0;
}

[簡單展示]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章