1. 程式人生 > >資料結構篇:校園最短路徑導航(三:地圖影象顯示以及完整程式)

資料結構篇:校園最短路徑導航(三:地圖影象顯示以及完整程式)

首先是地圖的顯示,因為控制檯限制,只能通過外部程式來顯示圖片

如果你使用的是VC6.0或者Dev C++將準備好的圖片放在工程根目錄,命名為SchoolMap.png

如果有小夥伴和我一樣使用的是CLion的話,就把圖片放到cmake-build-debug下面

在main函式裡呼叫以下語句即可顯示圖片

system("mspaint SchoolMap.png");

可是問題來了,如果呼叫這一個語句,程式會暫停,不把圖片關閉程式就不會繼續執行,還好控制檯會快取我們的輸入。也就有了挽救的可能。

    while(1){
        cout<<"請按照圖片選擇你想去的地方,輸入起始點和目的地的序號,以空格間隔。"<<endl;
        cout<<"標準格式 :0 1回車"<<endl;
        cout<<"若輸入已完成,請按回車鍵,並關閉圖片。即可顯示路徑。"<<endl;
        system("mspaint SchoolMap.png");
        cin>>originPos>>endPos;
        adjacencyList.ShowShortestResult(originPos,endPos);
    }

真的有點麻煩,我哭了,你們呢。

執行截圖

Gif版

完整程式

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;
//儲存最短路徑值
int ShortestPathvalue[32][32] = {0};
//儲存具體路徑
int ShortestPathmatrix[32][32] = {0};
//地點資訊
char _mapName[32][50] = {"行政樓", "實驗樓D", "教學樓A", "籃球場", "足球場", "A4", "實驗樓C", "教學樓B", "A2", "A6", "計算機系", "蘇果超市",
                         "果曼優品", "實驗樓A", "教學樓C", "圖書館", "一食堂", "D2", "D8", "C4", "中國聯通", "羽毛球場", "網球場",
                         "B5", "B7", "D4", "D6", "C8", "C6", "三食堂", "一鳴真鮮奶吧", "B11"};
//距離資訊,_distance[0][1] = 50;代表從下標為0到下表為1地點距離為50
int _distance[32][32] = {0};
//邊表結點
typedef struct EdgeNode {
    //頂點對應的下標
    int adjvex;
    //權值
    int weight;
    //指向下一個鄰接點
    struct EdgeNode *next;
} edgeNode;

//頂點表結點
typedef struct VertexNode {
    //頂點資料
    char data[50];
    //邊表頭指標
    edgeNode *firstedge;
} VertexNode, AdjList[100];

//集合
typedef struct {
    AdjList adjList;
    //頂點數和邊數
    int numVertexes, numEdges;
} GraphAdjList;

class AdjacencyList {
public:


    void ShowALGraph(GraphAdjList *G);

    void Test();

    //初始化地圖
    void InitMap(GraphAdjList *G);

    //建立地圖
    void CreateALGraph(GraphAdjList *G);

    //計算各個頂點之間最短路徑
    void ShortestPath_Floyd(GraphAdjList *G, int P[32][32], int D[32][32]);

    //輸出路徑長度和具體路徑
    void ShowShortestResult(int originPos,int endPos);
};

//建立地圖
void AdjacencyList::CreateALGraph(GraphAdjList *G) {
    edgeNode *e;
    //讀入頂點資訊,建立頂點表
    for (int i = 0; i < G->numVertexes; i++)
    {
        //讀入頂點資訊
        strcpy(G->adjList[i].data, _mapName[i]);
        //將邊表置為空表
        G->adjList[i].firstedge = NULL;
    }
    //建立邊表(頭插法)
    for (int i = 0; i < G->numVertexes; i++)
    {
        for (int j = 0; j < i; j++)
        {
            int temp;
            if (_distance[i][j] != 0 || _distance[j][i] != 0)
            {
                if (_distance[i][j] != 0)
                {
                    temp = _distance[i][j];
                    _distance[j][i] = _distance[i][j];
                }
                else
                {
                    temp = _distance[j][i];
                    _distance[i][j] = _distance[j][i];
                }
                e = new EdgeNode;
                e->adjvex = j;
                e->next = G->adjList[i].firstedge;
                e->weight = temp;
                G->adjList[i].firstedge = e;

                e = new EdgeNode;

                e->adjvex = i;
                e->next = G->adjList[j].firstedge;
                e->weight = temp;
                G->adjList[j].firstedge = e;
            }

        }
    }

}

void AdjacencyList::Test() {
    cout << "ALL IS OK." << endl;
}

void AdjacencyList::ShowALGraph(GraphAdjList *G) {
    for (int i = 0; i < G->numVertexes; i++)
    {
        cout << "頂點" << i << ": " << G->adjList[i].data << "--firstedge--";
        edgeNode *p = new edgeNode;
        p = G->adjList[i].firstedge;
        while (p)
        {
            cout << p->adjvex << "--Weight: " << p->weight << "--Next--";
            p = p->next;
        }
        cout << "--NULL" << endl;
    }

}

//初始化地圖基本資料
void AdjacencyList::InitMap(GraphAdjList *G) {
    //輸入頂點數和邊數
    G->numVertexes = 32;
    G->numEdges = 59;
    _distance[0][2] = 60;

    _distance[1][2] = 190;
    _distance[1][7] = 210;
    _distance[1][6] = 70;

    _distance[2][7] = 80;
    _distance[2][16] = 320;
    _distance[2][3] = 120;

    _distance[3][7] = 100;
    _distance[3][14] = 170;
    _distance[3][4] = 80;

    _distance[4][11] = 180;
    _distance[4][8] = 90;
    _distance[4][5] = 140;

    _distance[5][9] = 70;

    _distance[6][7] = 220;
    _distance[6][10] = 50;

    _distance[7][10] = 210;
    _distance[7][14] = 90;
    _distance[7][16] = 260;

    _distance[8][11] = 110;
    _distance[8][9] = 60;

    _distance[9][11] = 110;

    _distance[10][17] = 190;
    _distance[10][13] = 50;

    _distance[11][16] = 80;
    _distance[11][12] = 90;

    _distance[12][16] = 100;

    _distance[13][17] = 160;
    _distance[13][18] = 170;
    _distance[13][15] = 120;
    _distance[13][14] = 190;

    _distance[14][15] = 80;
    _distance[14][16] = 210;

    _distance[15][18] = 140;
    _distance[15][20] = 200;
    _distance[15][21] = 170;

    _distance[16][21] = 200;
    _distance[16][23] = 80;

    _distance[17][25] = 60;
    _distance[17][18] = 70;

    _distance[18][26] = 70;
    _distance[18][19] = 120;

    _distance[19][20] = 60;

    _distance[20][21] = 100;
    _distance[20][22] = 110;
    _distance[20][27] = 130;
    _distance[20][28] = 120;

    _distance[21][22] = 90;

    _distance[22][29] = 120;
    _distance[22][30] = 110;
    _distance[22][24] = 110;

    _distance[23][24] = 80;

    _distance[24][30] = 40;

    _distance[25][26] = 80;

    _distance[26][27] = 80;

    _distance[28][29] = 80;

    _distance[29][31] = 180;
    _distance[29][30] = 100;

    _distance[30][31] = 100;
}

void AdjacencyList::ShortestPath_Floyd(GraphAdjList *G, int P[32][32], int D[32][32]) {
    //初始化D與P
    for (int v = 0; v < G->numVertexes; ++v)
    {
        for (int w = 0; w < G->numVertexes; ++w)
        {
            if(_distance[v][w]==0&&v!=w){
                _distance[v][w] = 10000;
            }
            D[v][w] = _distance[v][w];
            P[v][w] = w;
        }
    }
    for (int k = 0; k < G->numVertexes; ++k)
    {
        for (int v = 0; v < G->numVertexes; ++v)
        {
            for (int w = 0; w < G->numVertexes; ++w)
            {
                if (D[v][w] > D[v][k] + D[k][w])
                {
                    D[v][w] = D[v][k] + D[k][w];
                    P[v][w] = P[v][k];
                }
            }
        }
    }

}

void AdjacencyList::ShowShortestResult(int originPos,int endPos) {
    int temp;
    cout << "地點" << _mapName[originPos] << "到地點" << _mapName[endPos] << "最短距離為" << ShortestPathvalue[originPos][endPos] << endl;
    temp = ShortestPathmatrix[originPos][endPos];
    cout<<"具體路徑為:"<<_mapName[originPos]<<"——>";
    while (temp!=endPos){
        cout<<_mapName[temp]<<"——>";
        temp = ShortestPathmatrix[temp][endPos];
    }
    cout<<_mapName[endPos]<<endl;
}


int main() {
    AdjacencyList adjacencyList;
    int originPos,endPos;
    GraphAdjList *GA = new GraphAdjList;
    adjacencyList.Test();
    adjacencyList.InitMap(GA);
    adjacencyList.CreateALGraph(GA);
    adjacencyList.ShortestPath_Floyd(GA,ShortestPathmatrix,ShortestPathvalue);
    cout<<"地圖所有資料已經初始化完成,地圖影象已顯示"<<endl;
    while(1){
        cout<<"請按照圖片選擇你想去的地方,輸入起始點和目的地的序號,以空格間隔。"<<endl;
        cout<<"標準格式 :0 1關閉圖片,回車"<<endl;
        cout<<"若輸入已完成,請關閉圖片。再按下回車鍵,即可顯示路徑。"<<endl;
        system("mspaint SchoolMap.png");
        cin>>originPos>>endPos;
        adjacencyList.ShowShortestResult(originPos,endPos);
    }

    return 0;
}