公鸡中的战斗机加油吧!
#pragma warning (disable:4996) # pragma comment(lib,"Winmm.lib") #include<stdio.h> #include<stdlib.h> #include<graphics.h> //引用EasyX图形库 #include<conio.h> #include<string.h> #include <windows.h> #define MaxVertexNum 100 //最大定点数设为100 #define WUQIONG 19980918 typedef char VertexType; typedef int EdgeType; int flag = 0; typedef struct //邻接矩阵 { VertexType vexs[MaxVertexNum]; //顶点表 EdgeType chexing[MaxVertexNum][MaxVertexNum]; //边表 int n, e; //顶点数和边数 }MGraph; MGraph GAO; struct lujing { int d; int lu[MaxVertexNum]; }P[MaxVertexNum]; struct xingxi { int bianhao; char mingcheng[20]; char jianjie[100]; }x[12]; void CreateMGraph(MGraph *G) { int i, j, k = 0; for (i = 0; i < 12; i++) for (j = 0; j < 12; j++) G->chexing[i][j] = WUQIONG; G->chexing[0][1] = G->chexing[1][0] = 150; G->chexing[0][9] = G->chexing[9][0] = 200; G->chexing[1][2] = G->chexing[2][1] = 20;//步行路线(二食堂-五教) G->chexing[2][3] = G->chexing[3][2] = 500; G->chexing[3][4] = G->chexing[4][3] = 100; G->chexing[4][5] = G->chexing[5][4] = 30; G->chexing[5][6] = G->chexing[6][5] = 30; G->chexing[6][7] = G->chexing[7][6] = 130;//步行路线(李园-桃园) G->chexing[7][9] = G->chexing[9][7] = 80; G->chexing[8][9] = G->chexing[9][8] = 100; G->chexing[9][10] = G->chexing[10][9] = 10; G->chexing[10][11] = G->chexing[11][10] = 200; G->chexing[10][4] = G->chexing[4][10] = 220;//步行路线(六教-一教) //G->chexing[10][2] = G->chexing[2][10] = 150; G->chexing[10][2] = G->chexing[2][10] = 40; G->chexing[11][3] = G->chexing[3][11] = 110;//步行路线(图书馆-篮球场) printf("欢迎使用,导入成功!!!\n"); system("pause"); return; } void dispath1(int dist[], int path[], int s[], int n, int v)//校门到各地的距离输出,v终点 { int i, k, j = 0, a[100]; for (i = 0; i < n; i++) if (s[i] == 1 && dist[i] < WUQIONG) { int m = 0, m1 = 0, m2 = 0, m3 = 0; k = i; a[j++] = k; while (k != v) { k = path[k]; a[j++] = k; } for (; j > 0; j--) { printf("%s->", x[a[j - 1]].mingcheng); if (strcmp(x[a[j - 1]].mingcheng, "二食堂") == 0) m++; if (strcmp(x[a[j - 1]].mingcheng, "五教") == 0)m++; if (strcmp(x[a[j - 1]].mingcheng, "李园") == 0) m1++; if (strcmp(x[a[j - 1]].mingcheng, "桃园") == 0) m1++; if (strcmp(x[a[j - 1]].mingcheng, "六教") == 0) m2++; if (strcmp(x[a[j - 1]].mingcheng, "一教") == 0) m2++; if (strcmp(x[a[j - 1]].mingcheng, "图书馆") == 0) m3++; if (strcmp(x[a[j - 1]].mingcheng, "篮球场") == 0) m3++; } printf(" 路径长度为:%d米\n\n", dist[i]); if (m == 2)printf("*(二食堂到五教有20米的步行距离,其余为车行)*\n\n"); if (m1 == 2)printf("*(李园到桃园有130米的步行距离,其余为车行)*\n\n"); if (m2 == 2)printf("*(六教到一教有220米的步行距离,其余为车行)*\n\n"); if (m3 == 2)printf("*(图书馆到篮球场有110米的步行距离,其余为车行)*\n\n"); } else printf("%s<-%s不存在路径\n", x[i].mingcheng, x[v].mingcheng); } void dispath2(int dist[], int path[], int s[], int n, int v, int m)//int m 是dispath2()终点编号,任意两地的距离输出 { int k, j = 0, b[100]; if (s[m] == 1 && dist[m] < WUQIONG) { int g = 0, g1 = 0, g2 = 0, g3 = 0; k = m; b[j++] = k; printf("%s到%s的最短路径为: ", x[v].mingcheng, x[m].mingcheng); while (k != v) { k = path[k]; b[j++] = k; } for (; j > 0; j--) { printf("%s->", x[b[j - 1]].mingcheng); if (strcmp(x[b[j - 1]].mingcheng, "二食堂") == 0) g++; if (strcmp(x[b[j - 1]].mingcheng, "五教") == 0)g++; if (strcmp(x[b[j - 1]].mingcheng, "李园") == 0) g1++; if (strcmp(x[b[j - 1]].mingcheng, "桃园") == 0) g1++; if (strcmp(x[b[j - 1]].mingcheng, "六教") == 0) g2++; if (strcmp(x[b[j - 1]].mingcheng, "一教") == 0) g2++; if (strcmp(x[b[j - 1]].mingcheng, "图书馆") == 0) g3++; if (strcmp(x[b[j - 1]].mingcheng, "篮球场") == 0) g3++; } printf(" 路径长度为:%d米\n\n", dist[m]); if (g == 2)printf("*(二食堂到五教有20米的步行距离,其余为车行)*\n\n"); if (g1 == 2)printf("*(李园到桃园有130米的步行距离,其余为车行)*\n\n"); if (g2 == 2)printf("*(六教到一教有220米的步行距离,其余为车行)*\n\n"); if (g3 == 2)printf("*(图书馆到篮球场有110米的步行距离,其余为车行)*\n\n"); } else printf("%s<-%s不存在路径\n", x[m].mingcheng, x[v].mingcheng); } void init() { int dist[MaxVertexNum], path[MaxVertexNum]; int s[MaxVertexNum]; int b[MaxVertexNum]; } void dijkstra(MGraph *G, int v, int n, int key) //int n是dispath2()终点编号,int key是两种输出的选择,key==0是校门到各地的距离key==1是任意两点距离 { int dist[MaxVertexNum], path[MaxVertexNum]; int s[MaxVertexNum]; int b[MaxVertexNum]; int mindis; int l; int flag = 1,flag1=0; for (l = 0; l < 2; l++) { int i, j, k; int m = 0; for (i = 0; i < 12; i++) { dist[i] = G->chexing[v][i]; s[i] = 0; if (G->chexing[v][i] < WUQIONG) path[i] = v; else path[i] = -1;//说明两点之间没路 } s[v] = 1; for (i = 0; i < 12; i++) { mindis = WUQIONG; k = v; for (j = 0; j < 12; j++)//找最小的一点 { if (s[j] == 0 && dist[j] < mindis) { k = j; mindis = dist[j]; } } s[k] = 1; for (j = 0; j < 12; j++) { if (s[j] == 0 && G->chexing[k][j] < WUQIONG&&dist[k] + G->chexing[k][j] < dist[j]) { dist[j] = dist[k] + G->chexing[k][j]; path[j] = k; } if (l == 1) { if (dist[k] + G->chexing[k][j] == dist[n] && l == 1) { dist[j] = dist[k] + G->chexing[k][j]; path[j] = k; //dispath2(dist, path, s, 12, v, n); } } } } for (i = 0; i < 12; i++) { if (l == 0 && flag == 1) { b[i] = path[i]; } } if (key == 1) { //dispath1(dist, path, s, 12, v); //} //else { for (i = 0; i < 12; i++) { if (path[i] != b[i]) { dispath2(dist, b, s, 12, v, n); //system("pause"); dispath2(dist, path, s, 12, v, n); flag = 0; } else flag1 = 1; } //m = dist[n]; //printf("%d", m); } } if (key == 0 ) { dispath1(dist, path, s, 12, v); } if (flag1 == 1&&flag!=0) dispath2(dist, b, s, 12, v, n); system("pause"); } void floyd(MGraph *G) { int i = 0, j = 0; printf("请输入起点对应编号!!!\n"); scanf("%d", &i); printf("请输入终点对应编号!!!\n"); scanf("%d", &j); if (i >= 0 && i <= 12 && j >= 0 && j <= 12) dijkstra(&GAO, i, j, 1); else { printf("请输入0-12范围的数\n"); system("pause"); return; } } void startup() { x[0].bianhao = 0; strcpy(x[0].mingcheng, "校门"); strcpy(x[0].jianjie, "湖北汽车工业学院位于中国汽车城—湖北省十堰市,\n是全国唯一一所以汽车命名的以工学为主全日制普通本科院校。"); x[1].bianhao = 1; strcpy(x[1].mingcheng, "二食堂"); strcpy(x[1].jianjie, "二食堂的杜婆鸡"); x[2].bianhao = 2; strcpy(x[2].mingcheng, "五教"); strcpy(x[2].jianjie, "第五教学楼"); x[3].bianhao = 3; strcpy(x[3].mingcheng, "图书馆"); strcpy(x[3].jianjie, "逸夫图书馆"); x[4].bianhao = 4; strcpy(x[4].mingcheng, "六教"); strcpy(x[4].jianjie, "电气与信息工程学院"); x[5].bianhao = 5; strcpy(x[5].mingcheng, "李园食堂"); strcpy(x[5].jianjie, "李园食堂一楼的热干面"); x[6].bianhao = 6; strcpy(x[6].mingcheng, "李园"); strcpy(x[6].jianjie, "男生的天下"); x[7].bianhao = 7; strcpy(x[7].mingcheng, "桃园"); strcpy(x[7].jianjie, "桃园生活广场"); x[8].bianhao = 8; strcpy(x[8].mingcheng, "西区公寓"); strcpy(x[8].jianjie, "科技学院宿舍楼"); x[9].bianhao = 9; strcpy(x[9].mingcheng, "足球场"); strcpy(x[9].jianjie, "四百米跑道"); x[10].bianhao = 10; strcpy(x[10].mingcheng, "一教"); strcpy(x[10].jianjie, "第一教学楼"); x[11].bianhao = 11; strcpy(x[11].mingcheng, "篮球场"); strcpy(x[11].jianjie, "很大的篮球场"); } void caidan() { initgraph(600, 500); IMAGE img_bk; loadimage(&img_bk, "d:\\view.JPG"); putimage(0, 0, &img_bk); mciSendString("play bk", NULL, 0, NULL); Sleep(4000); closegraph(); return; } void chaxun() { int no = 0; printf("请输入要查询的景点编号\n"); scanf("%d", &no); if (no >= 0 && no <= 12) { printf("编号:%d\n名称:%s\n简介:%s\n", x[no].bianhao, x[no].mingcheng, x[no].jianjie); } else printf("请输入正确的景点编号\n"); system("pause"); return; } void updateWithoutInput() { system("cls"); printf("*****湖北汽车工业学院校园导游咨询与最短路径****\n"); printf("\n\n"); printf(" 菜单\n\n"); printf(" 1.地图导入 "); printf("*0.校门 1.二食堂 2.五教\n"); printf(" 2.景点简介 "); printf(" 3.图书馆 4.六教 5.李园餐厅\n"); printf(" 3.校门到各地距离 "); printf(" 6.李园 7.桃园 8.西区公寓\n"); printf(" 4.两点间距离 "); printf(" 9.足球场 10.一教 11.篮球场\n"); printf(" 5.查看地图\n"); return; } void updateWithInput() { char input; if (kbhit()) { input = getch(); if (input == '1') { CreateMGraph(&GAO); flag = 1; } else if (input != '1'&&flag == 0) { printf("请先选一导入地图再进行其他操作\n"); system("pause"); } if (flag == 1) { if (input == '2') chaxun(); if (input == '3') dijkstra(&GAO, 0, 0, 0); if (input == '4') floyd(&GAO); if (input == '5') caidan(); } } } int main() { startup(); while (1) { updateWithoutInput(); updateWithInput(); } system("pause"); return 0; }