資料結構篇:校園最短路徑導航(一:地圖資料的配置以及圖的建立)
阿新 • • 發佈:2018-12-21
首先去找一張學校的地圖,並且自己配置好資料和路線
在程式碼裡面寫好資料
//地點資訊 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};
然後是圖的結構,僅僅與不帶權鄰接表在邊表結點相差一個weight
//邊表結點 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;
初始化地圖距離資訊
//初始化地圖基本資料 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; }
開始建立地圖 ,注意,由於我們只賦值了一條邊一次值,但從常理上說,他應該有2次,因為無向鄰接表就是雙向鄰接表,所以我們建立地圖演算法要多做一下判定。
//建立地圖
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];
}
else
{
temp = _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;
}
}
}
}
執行程式檢驗是否成功