校园导游系统 数据结构课程设计
郑州师范学院老校区校园导游系统
尴尬的最短路算法
Floyd and Dijkstra
尴尬的数据结构......
记录一下代码:
多有参考.....
#include
#include
#include
#include
#define MaxSize 100
#define VISITED 1
#define NOTVISITED 0
#define Infinite 1073741823
#define MaxViewNum 50 /*景点个数最大50*/
#define MaxRoad 1000 /*定义路径为无穷大*/
#define N 16 /*目前景点个数*/
typedef struct
{
char name[30]; /*景点名称*/
char number[10]; /*景点代号*/
char introduce[200]; /*景点介绍*/
}Elemtype; /*景点信息*/
typedef struct
{
int num; /*景点编号*/
Elemtype date; /*景点信息*/
}View; /*定义景点*/
typedef struct
{
View view[MaxViewNum]; /*存放顶点的一维数组,数组第零单元没有用上*/
int length[MaxViewNum][MaxViewNum]; /*存放路径长度*/
int m,n;
}MGraph;
MGraph MGr; /*全局变量,定义MGr为MGraph类型*/
int shortest[MaxViewNum][MaxViewNum]; /*定义全局变量存贮最小路径*/
int path[MaxViewNum][MaxViewNum]; /*定义存贮路径*/
void init()
{
int i,j;
MGr.view[1].num=1;
strcpy(MGr.view[1].date.name,"学校正门");
strcpy(MGr.view[1].date.number,"001");
strcpy(MGr.view[1].date.introduce,"坐落于英才街,对面为国基生活园区,市井繁华,交通便利。");
MGr.view[2].num=2;
strcpy(MGr.view[2].date.name,"行政楼");
strcpy(MGr.view[2].date.number,"002");
strcpy(MGr.view[2].date.introduce,"学校领导,办公主楼。");
MGr.view[3].num=3;
strcpy(MGr.view[3].date.name,"教学区");
strcpy(MGr.view[3].date.number,"003");
strcpy(MGr.view[3].date.introduce,"1-5号教学楼,包含信息学院以及化学,物理学院等。");
MGr.view[4].num=4;
strcpy(MGr.view[4].date.name,"天鹅湖");
strcpy(MGr.view[4].date.number,"004");
strcpy(MGr.view[4].date.introduce,"郑州师范学院老校区的一大景点,黑白天鹅,戏水相融。");
MGr.view[5].num=5;
strcpy(MGr.view[5].date.name,"图书馆");
strcpy(MGr.view[5].date.number,"005");
strcpy(MGr.view[5].date.introduce,"历史悠久,文化积淀。供同学安静学习的环境。");
MGr.view[6].num=6;
strcpy(MGr.view[6].date.name,"艺术之地");
strcpy(MGr.view[6].date.number,"006");
strcpy(MGr.view[6].date.introduce,"美术学院和音乐与舞蹈学院,通往艺术的殿堂。");
MGr.view[7].num=7;
strcpy(MGr.view[7].date.name,"运动场");
strcpy(MGr.view[7].date.number,"007");
strcpy(MGr.view[7].date.introduce,"绿茵地、活动中心。篮球场、足球场、网球场依次,丰富课余生活。");
MGr.view[8].num=8;
strcpy(MGr.view[8].date.name,"餐厅");
strcpy(MGr.view[8].date.number,"008");
strcpy(MGr.view[8].date.introduce,"餐厅,有民族餐厅的免费的开水房,老校区同学们吃饭的地方。饭菜是否可口就......");
MGr.view[9].num=9;
strcpy(MGr.view[9].date.name,"校医院");
strcpy(MGr.view[9].date.number,"009");
strcpy(MGr.view[9].date.introduce,"校医院,同学们看病拿药的地方,提供便利。");
MGr.view[10].num=10;
strcpy(MGr.view[10].date.name,"老校区西门");
strcpy(MGr.view[10].date.number,"010");
strcpy(MGr.view[10].date.introduce,"西门出去正对文化路,对面就是河南牧业经济学院。");
MGr.view[11].num=11;
strcpy(MGr.view[11].date.name,"特殊教育学院");
strcpy(MGr.view[11].date.number,"011");
strcpy(MGr.view[11].date.introduce,"特殊教育学院,其中聋哑人居多,为每一个学生提供合适平等的教学场所。");
MGr.view[12].num=12;
strcpy(MGr.view[12].date.name,"学生公寓");
strcpy(MGr.view[12].date.number,"012");
strcpy(MGr.view[12].date.introduce,"学生宿舍区,其中8号楼为女生宿舍,9号楼为男生宿舍。");
MGr.view[13].num=13;
strcpy(MGr.view[13].date.name,"郑州市自然博物馆");
strcpy(MGr.view[13].date.number,"013");
strcpy(MGr.view[13].date.introduce,"郑州市自然博物馆,坐落于郑州师范学院老校区西北角,历史悠久。");
MGr.view[14].num=14;
strcpy(MGr.view[14].date.name,"初教院教学楼");
strcpy(MGr.view[14].date.number,"014");
strcpy(MGr.view[14].date.introduce,"初教院,初等教育学院的学生日常上课的地方。");
MGr.view[15].num=15;
strcpy(MGr.view[15].date.name,"中州北门");
strcpy(MGr.view[15].date.number,"015");
strcpy(MGr.view[15].date.introduce,"相邻的中州大学的北门,出门为开元路,右转直走即可到郑州师范学院新校区。");
MGr.view[16].num=16;
strcpy(MGr.view[16].date.name,"中州大学教学区");
strcpy(MGr.view[16].date.number,"016");
strcpy(MGr.view[16].date.introduce,"该地区为中州大学教学区域。");
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
MGr.length[i][j]=MaxRoad;
}
}
for(i=1;i<=N;i++)
{
shortest[i][j]=0;
}
MGr.length[1][2]=MGr.length[2][1]=50;
MGr.length[2][3]=MGr.length[3][2]=50;
MGr.length[3][4]=MGr.length[4][3]=60;
MGr.length[3][5]=MGr.length[5][3]=100;
MGr.length[5][6]=MGr.length[6][5]=100;
MGr.length[6][7]=MGr.length[7][6]=220;
MGr.length[6][9]=MGr.length[9][6]=80;
MGr.length[7][8]=MGr.length[8][7]=250;
MGr.length[8][9]=MGr.length[9][8]=100;
MGr.length[8][10]=MGr.length[10][8]=150;
MGr.length[9][11]=MGr.length[11][9]=50;
MGr.length[10][11]=MGr.length[11][10]=60;
MGr.length[11][12]=MGr.length[12][11]=40;
MGr.length[12][13]=MGr.length[13][12]=50;
MGr.length[13][14]=MGr.length[14][13]=20;
MGr.length[11][14]=MGr.length[14][11]=80;
MGr.length[16][14]=MGr.length[14][16]=500;
MGr.length[13][15]=MGr.length[15][13]=450;
MGr.length[2][16]=MGr.length[16][2]=150;
MGr.length[1][1]=MGr.length[2][2]=MGr.length[3][3]=MGr.length[4][4]=0;
MGr.length[5][5]=MGr.length[6][6]=MGr.length[7][7]=MGr.length[8][8]=0;
MGr.length[9][9]=MGr.length[10][10]=MGr.length[11][11]=MGr.length[12][12]=0;
MGr.length[13][13]=MGr.length[14][14]=MGr.length[15][15]=MGr.length[16][16]=0;
}
void introduce()
{
int m;
printf("请输入查询景点编号:\n");
scanf("%d",&m); fflush(stdin);
switch(m)
{
case 1:
printf("景点编号:%s\t",MGr.view[1].date.number);
printf("景点名称:%s\n",MGr.view[1].date.name);
printf("景点简介:%s\n",MGr.view[1].date.introduce);
break;
case 2:
printf("景点编号:%s\t",MGr.view[2].date.number);
printf("景点名称:%s\n",MGr.view[2].date.name);
printf("景点简介:%s\n",MGr.view[2].date.introduce);
break;
case 3:
printf("景点编号:%s\t",MGr.view[3].date.number);
printf("景点名称:%s\n",MGr.view[3].date.name);
printf("景点简介:%s\n",MGr.view[3].date.introduce);
break;
case 4:
printf("景点编号:%s\t",MGr.view[4].date.number);
printf("景点名称:%s\n",MGr.view[4].date.name);
printf("景点简介:%s\n",MGr.view[4].date.introduce);
break;
case 5:
printf("景点编号:%s\t",MGr.view[5].date.number);
printf("景点名称:%s\n",MGr.view[5].date.name);
printf("景点简介:%s\n",MGr.view[5].date.introduce);
break;
case 6:
printf("景点编号:%s\t",MGr.view[6].date.number);
printf("景点名称:%s\n",MGr.view[6].date.name);
printf("景点简介:%s\n",MGr.view[6].date.introduce);
break;
case 7:
printf("景点编号:%s\t",MGr.view[7].date.number);
printf("景点名称:%s\n",MGr.view[7].date.name);
printf("景点简介:%s\n",MGr.view[7].date.introduce);
break;
case 8:
printf("景点编号:%s\t",MGr.view[8].date.number);
printf("景点名称:%s\n",MGr.view[8].date.name);
printf("景点简介:%s\n",MGr.view[8].date.introduce);
break;
case 9:
printf("景点编号:%s\t",MGr.view[9].date.number);
printf("景点名称:%s\n",MGr.view[9].date.name);
printf("景点简介:%s\n",MGr.view[9].date.introduce);
break;
case 10:
printf("景点编号:%s\t",MGr.view[10].date.number);
printf("景点名称:%s\n",MGr.view[10].date.name);
printf("景点简介:%s\n",MGr.view[10].date.introduce);
break;
case 11:
printf("景点编号:%s\t",MGr.view[11].date.number);
printf("景点名称:%s\n",MGr.view[11].date.name);
printf("景点简介:%s\n",MGr.view[11].date.introduce);
break;
case 12:
printf("景点编号:%s\t",MGr.view[12].date.number);
printf("景点名称:%s\n",MGr.view[12].date.name);
printf("景点简介:%s\n",MGr.view[12].date.introduce);
break;
case 13:
printf("景点编号:%s\t",MGr.view[13].date.number);
printf("景点名称:%s\n",MGr.view[13].date.name);
printf("景点简介:%s\n",MGr.view[13].date.introduce);
break;
case 14:
printf("景点编号:%s\t",MGr.view[14].date.number);
printf("景点名称:%s\n",MGr.view[14].date.name);
printf("景点简介:%s\n",MGr.view[14].date.introduce);
break;
case 15:
printf("景点编号:%s\t",MGr.view[15].date.number);
printf("景点名称:%s\n",MGr.view[15].date.name);
printf("景点简介:%s\n",MGr.view[15].date.introduce);
break;
case 16:
printf("景点编号:%s\t",MGr.view[16].date.number);
printf("景点名称:%s\n",MGr.view[16].date.name);
printf("景点简介:%s\n",MGr.view[16].date.introduce);
break;
default:
printf("输入序号错误。\n");
break;
}
printf("\n");
}
void floyd() /*佛洛依德算法*/
{
int i,j,k;
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
shortest[i][j]=MGr.length[i][j];
path[i][j]=0;
}
} /*初始化数组*/
for(k=1;k<=N;k++)
{
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
if(shortest[i][j]>(shortest[i][k]+shortest[k][j]))
{
shortest[i][j]=shortest[i][k]+shortest[k][j];
path[i][j]=k;
path[j][i]=k; /*记录经过的路径*/
}//end_if
}
}//end_for
}
}
void display(int i,int j)
{ /*打印两个景点的路径及最短路径*/
int a,b;
a=i;
b=j;
printf("您要查询的两景点间最短路径是: \n\n");
fflush(stdin);
if(i%d的最短路径是: %d 米。\n\n",a,b,shortest[a][b]);
}
else
{
printf("%d",a);
while(path[i][j]!=0)
{ /*把i到j的路径上所有经过的景点按顺序打印出来*/
printf("-->%d",path[i][j]);
if(i%d\n\n",b);
printf("%d->%d的最短路径是: %d 米。\n\n",a,b,shortest[a][b]);
}
}/*display*/
int shortdistance()
{ /*要查找的两景点的最短路径*/
int i,j;
printf("请输入要查询的两个景点的数字编号(1->16)中间用空格间隔开。\n");
scanf("%d %d",&i,&j);
if(i>N||i<0||j>N||j<0)
{
printf("输入信息错误!\n\n");
printf("请输入要查询的两个景点的数字编号(1->16)中间用空格间隔开。\n");
scanf("%d %d",&i,&j);
}
else
{
floyd();
display(i,j);
}
return 1;
fflush(stdin);
}/*shortestdistance*/
int A[MaxSize+1][MaxSize+1]; /*迪杰斯特拉算法*/
int D[MaxSize+1];
int S[MaxSize+1],P[MaxSize+1];
int source,sink;
int step = 1;
int top = -1;
int Stack[MaxSize+1];
void input()
{
int i;
printf("\n请输入起始节点:");
scanf("%d",&source);
printf("\n请输入结束节点:");
scanf("%d",&sink);
for ( i = 1; i <= N; i++ )
{
S[i] = NOTVISITED;
D[i] = MGr.length[source][i];
P[i] = source;
}
S[source] = VISITED;
D[source] = 0;
}
void Push(int value)
{
if ( top >= MaxSize )
{
printf("没有路径存在!\n\n");
exit(1);
}
else
Stack[++top] = value;
}
int Pop()
{
if ( top < 0 )
{
printf("没有路径存在!\n\n");
exit(1);
}
return Stack[top--];
}
int minD()
{
int i,t=0;
long int minimum = Infinite;
for ( i=1;i<=N;i++ )
if ( (S[i] == NOTVISITED) && D[i] < minimum )
{
minimum = D[i];
t = i;
}
return t;
}
void output_path()
{
int node = sink;
if ( (sink == source) || (D[sink] == Infinite) )
{
printf("\n节点%d与节点%d之间没有路径存在!\n\n",source,sink);
return;
}
printf("\n");
while ( node != source )
{
Push(node);
node = P[node];
}
printf("V%d到V%d的最短路径为:\n",source,sink);
printf(" V%d",source);
while( node != sink)
{
node = Pop();
printf(" --%ld-->",MGr.length[ P[node] ][node]);
printf("V%d",node);
}
printf("\n");
printf("\n %d->%d的最短路径是: %ld\n",source,sink,D[sink]);
printf("\n");
}
void distance()
{
int t,I;
input();
for ( step =2;step <=N; step++ )
{
t = minD();
S[t] = VISITED;
for ( I=1; I <= N; I++ )
if ( (S[I] == NOTVISITED) && (D[t]+MGr.length[t][I] <= D[I]) )
{
D[I] = D[t] + MGr.length[t][I];
P[I] = t;
}
}
output_path();
}
void map()
{
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃┏━━━━━━━━┓┏━━━━━┓ ┏━━━━━━━┓ ┃\n");
printf("┃┃13.自然博物馆 ┃┃14.初教院 ┃ ━━━┫15.中州北门 ┣━┃\n");
printf("┃┗━━━━━━━━┛┗━━━━━┛ ┗━━━━━━━┛ ┃\n");
printf("┃ ┃\n");
printf("┃ ┏━━━━━┓ ┃\n");
printf("┃ ┃ 12. ┃ ┏━━━━┓ ┃\n");
printf("┃ ┃ 学生公寓 ┃ ┃8.餐厅 ┃ ┃\n");
printf("┃ ┗━━━━━┛ ┗━━━━┛ ┃\n");
printf("┃┏━━━━━━━━━━━━━━┓ ┏━━━━┓ ┃\n");
printf("┃┃ 11. 特教院 ┃ ┃9.校医院┃ ┏━━━━━━┓ ┃\n");
printf("┃┗━━━━━━━━━━━━━━┛ ┗━━━━┛ ┃ 16. ┃ ┃\n");
printf("┃┏━━━━┓ ┃ 中州 ┃ ┃\n");
printf("┃┃10.西门 ┃ ┃ 教学区 ┃ ┃\n");
printf("┃┗━━━━┛ ┃ ┃ ┃\n");
printf("┃ ┏━━━━━━━━━┓ ┏━━━━━━┓ ┗━━━━━━┛ ┃\n");
printf("┃ ┃ ┃ ┃6.美术学院 ┃ ┃\n");
printf("┃ ┃ ┃ ┃与音乐学院 ┃ ┃\n");
printf("┃ ┃ 7. 运 ┃ ┗━━━━━━┛ ┃\n");
printf("┃ ┃ ┃ ┃\n");
printf("┃ ┃ 动 ┃ ┏━━━━┓ ┃\n");
printf("┃ ┃ ┃ ┃ 4. ┃ ┃\n");
printf("┃ ┃ 场 ┃ ┃ 天鹅湖 ┃ ┃\n");
printf("┃ ┃ ┃ ┗━━━━┛ ┏━━━━━━┓ ┃\n");
printf("┃ ┃ ┃ ┃ 图 ┃ ┃\n");
printf("┃ ┃ ┃ ┏━━━━━┓ ┃ 5. 书 ┃ ┃\n");
printf("┃ ┃ ┃ ┃四五号楼 ┃ ┃ 馆 ┃ ┃\n");
printf("┃ ┗━━━━━━━━━┛ ┃━━━━━┃ ┗━━━━━━┛ ┃\n");
printf("┃ ┃二三号楼 ┃ ┃\n");
printf("┃ ┃━━━━━┃ ┃\n");
printf("┃ ┃3.一号楼 ┃ ┃\n");
printf("┃ ┗━━━━━┛ ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ┏━━━━━━━━━┓ ┏━━━━━┓ ┃\n");
printf("┃ ┃ 2. 行 政 楼 ┃ ━━━━━━━┫1.学校正门┣━━━━━━┃\n");
printf("┃ ┗━━━━━━━━━┛ ┗━━━━━┛ ┃\n");
printf("┃ ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\n");
fflush(stdin);
}/*map*/
int main()
{
char k;
init();
printf("**********************************************************************\n");
printf("* *\n");
printf("* *\n");
printf("* 欢迎使用郑州师范学院老校区导游系统 ! *\n");
printf("* *\n");
printf("* *\n");
printf("**********************************************************************\n");
printf("\n");
while(1)
{
printf("1.景点信息查询请按“1”键;\n");
printf("2.景点最短路径查询(佛洛依德算法)请按“2”键;\n");
printf("3.景点最短路径查询(迪杰斯特拉算法)请按“3”键;\n");
printf("4.校内景点地图查询请按“4”键;\n");
printf("5.退出系统请按“5”键;\n");
printf("请选择: \n");
scanf("%c",&k);
switch(k)
{
case '1':printf("景点介绍查询(请输入1-16)。\n");
introduce();break;
case '2':printf("景点最短路径查询(佛洛依德算法)。");
shortdistance();break;
case '3':printf("景点最短路径(迪杰斯特拉算法)查询。");
distance();break;
case '4':printf("景点地图。\n");
map();break;
case '5':printf("谢谢使用!\n");exit(0);
}
}
system("pause");
return 0;
}