基于Floyd的校园导航小程序

基于Floyd的校园导航小程序

使用了邻接表和Floyd,将schoolmap.png的路径改为自己的图的路径即可用。

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

string mapName[37] = {"保卫处", "煤炭科技中心与结构实验中心", "16、17、18教", "15教", "14教", "11、12、13教",
                      "9、10教", "大学生创新创业与就业基地", "行政楼", "2教", "3教", "新馆入口", "旧馆入口",
                      "实验楼", "1教", "明德广场", "校门口", "滨河路入口", "3、4、5号学生公寓", "开水房", "配电室",
                      "学生超市", "1、2号学生公寓", "榴馨苑", "2号教师公寓", "洗浴中心", "骊绣苑", "综合楼",
                      "十字路口", "6、7号学生公寓", "篮球场", "10、11、12、13号学生公寓", "8、9号学生公寓",
                      "14、15、16号学生公寓", "游泳馆", "主田径场", "综合文体馆"
                     };


//最短值
int Distance[37][37] = {0}; //Distance[i][j] = m

//存储路径
int path[37][37] = {0};

//边表结点
struct arcNode {

	//顶点对应的下标
	int m_adjvex;
	//权值
	int m_weight;
	//指向下一个邻接点
	arcNode *m_next;
};

//顶点表结点
typedef struct VertexNode {
	//顶点数据
	string m_data;
	//边表头指针
	arcNode *m_firstedge;
} VertexNode, AdjList[100];

//集合
struct graphAdjList {

	AdjList adjList;
	//顶点数和边数
	int vexnum, arcnum;
};


void initMap(graphAdjList &AMG) {

	AMG.vexnum = 37;
	AMG.arcnum = 51;
	Distance[1][9] = 55;
	Distance[1][2] = 35;
	Distance[2][3] = 65;
	Distance[2][8] = 110;
	Distance[2][11] = 65;
	Distance[3][4] = 30;
	Distance[4][5] = 35;
	Distance[5][6] = 70;
	Distance[6][7] = 105;
	Distance[7][36] = 60;
	Distance[8][37] = 150;
	Distance[9][10] = 70;
	Distance[9][17] = 200;
	Distance[10][11] = 40;
	Distance[11][12] = 115;
	Distance[11][16] = 50;
	Distance[12][28] = 45;
	Distance[12][23] = 165;
	Distance[12][18] = 90;
	Distance[13][16] = 45;
	Distance[13][28] = 55;
	Distance[14][16] = 50;
	Distance[14][15] = 40;
	Distance[14][18] = 81;
	Distance[15][17] = 240;
	Distance[15][18] = 93;
	Distance[18][21] = 130;
	Distance[19][22] = 48;
	Distance[19][24] = 36;
	Distance[20][21] = 38;
	Distance[20][22] = 96;
	Distance[20][25] = 150;
	Distance[22][25] = 56;
	Distance[22][26] = 41;
	Distance[23][24] = 15;
	Distance[24][27] = 100;
	Distance[24][26] = 35;
	Distance[25][30] = 150;
	Distance[26][27] = 51;
	Distance[27][32] = 150;
	Distance[27][30] = 90;
	Distance[28][29] = 40;
	Distance[29][30] = 35;
	Distance[30][36] = 110;
	Distance[30][31] = 50;
	Distance[31][33] = 70;
	Distance[31][32] = 50;
	Distance[33][34] = 40;
	Distance[34][35] = 80;
	Distance[35][36] = 80;
	Distance[36][37] = 90;

}

//创建地TU
void createALGraph(graphAdjList &G) {
	cout << "开始创建地图…………" << endl;
	arcNode *e;
	//读入顶点信息,建立顶点表
	for (int i = 0; i < G.vexnum; i++) {
		//读入顶点信息
		G.adjList[i].m_data = mapName[i];
		//将边表置为空表
		G.adjList[i].m_firstedge = NULL;
	}
	//头插法建立边表
	for (int i = 0; i < G.vexnum; 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 arcNode();
				e->m_adjvex = j;
				e->m_next = G.adjList[i].m_firstedge;
				e->m_weight = temp;
				G.adjList[i].m_firstedge = e;

				e = new arcNode();

				e->m_adjvex = i;
				e->m_next = G.adjList[j].m_firstedge;
				e->m_weight = temp;
				G.adjList[j].m_firstedge = e;
			}

		}
	}
	cout << "创建成功!" << endl;
}

void showAlgraph(graphAdjList &G) {

	for (int i = 0; i < G.vexnum; i++) {

		cout << "地点" << i << ": " << G.adjList[i].m_data << "->firstedge->";
		arcNode *p = new arcNode;
		p = G.adjList[i].m_firstedge;
		while (p) {

			cout << p->m_adjvex << " (time: " << p->m_weight << "s)  nextNode->";
			p = p ->m_next;
		}
		cout << "->end;" << endl << endl;
	}
}

//利用floyd 对 path[][]、shortestDistance[][]
void floyd(graphAdjList &G) {

	for (int m = 0; m < G.vexnum; m++) {
		for (int n = 0; n < G.vexnum; n++) {
			if (Distance[m][n] == 0 && m != n)
				Distance[m][n] = 99999;
			path[m][n] = n;
		}
	}
	for (int k = 0; k < G.vexnum; k++) {
		for (int m = 0; m < G.vexnum; m++) {
			for (int n = 0; n < G.vexnum; n++) {
				if (Distance[m][n] > Distance[m][k] + Distance[k][n]) {
					Distance[m][n] = Distance[m][k] + Distance[k][n];
					path[m][n] = path[m][k];
				}
			}
		}
	}
}


void showPath(int startAdd, int endAdd) {

	int tempAdd;
	cout << mapName[startAdd] << "-->" << mapName[endAdd] << ": " << endl;
	cout << "① 步行用时最短为" << Distance[startAdd][endAdd] << "s" << endl;
	tempAdd = path[startAdd][endAdd];
	cout << "② 路径为: " << mapName[startAdd] << "-->";
	while (tempAdd != endAdd) {
		cout << mapName[tempAdd] << "-->";
		tempAdd = path[tempAdd][endAdd];
	}
	cout << mapName[endAdd] << endl;
}

void maintest() {

	graphAdjList myGraph;
	initMap(myGraph);
	createALGraph(myGraph);
	floyd(myGraph);
	cout << "请关闭地图后再进行操作!!!" << endl;
	system("mspaint schoolMap.png");
	int choose = -1;
	while (choose != 0) {
		cout << "--------------欢迎来到校园导航系统------------" << endl;
		cout << "--------------1.查看整个地图路径--------------" << endl;
		cout << "--------------2.查找最优路径------------------" << endl;
		cout << "--------------0.退出程序----------------------" << endl;
		cout << "----------------------------------------------" << endl;
		cout << "请输入您的选择(1/2/3) : " << endl;
		cin >> choose;
		switch (choose) {

			case 1: {
				system("cls");
				showAlgraph(myGraph);
				cout << "该操作完成后请关闭地图后进行下一步!" << endl;
				system("mspaint schoolMap.png");
				break;
			}
			case 2: {
				system("cls");
				int startAdd, endAdd;
				cout << "请通过地图选择你的起始点后,关闭地图进行输入操作!" << endl;
				system("mspaint schoolMap.png");
				cout << "请输入 起始地点序号 和 结束地点序号:" << endl;
				cin >> startAdd >> endAdd;
				startAdd--;
				endAdd--;
				showPath(startAdd, endAdd);
				break;
			}
			case 0:
				break;
			default: {
				system("cls");
				cout << "输入不合法!" << endl;
				break;
			}
		}
		system("pause");
		system("cls");
	}
}

int main() {

	maintest();
	system("pause");
	return 0;
}
/*
1. 保卫处
2. 煤炭科技中心与结构实验中心
3. 16、17、18号教学楼
4. 15号教学楼
5. 14号教学楼
6. 11、12、13号教学楼
7. 9、10号教学楼
8. 大学生创新创业与就业基地
9. 行政楼
10. 2号教学楼
11. 3号教学楼
12. 新馆入口
13. 旧馆入口
14. 实验楼
15. 1教
16. 明德广场
17. 校门口
18. 滨河路入口
19. 3、4、5号学生公寓
20. 开水房
21. 配电室
22. 学生超市
23. 1、2号学生公寓
24. 榴馨苑
25. 2号教师公寓
26. 洗浴中心
27. 骊绣苑
28. 综合楼
29. 十字路口
30. 6、7号学生公寓
31. 篮球场
32. 10、11、12、13号学生公寓
33. 8、9号学生公寓
34. 14、15、16号学生公寓
35. 游泳馆
36. 主田径场
37. 综合文体馆
*/
  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值