#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#define INT_MAX 10000
#define n 9
int a,d,s[100];
int cost[n][n]; /* 边的值*/
int shortest[n][n];
int path[n][n];/* 经过的景点*/
void introduce();
int shortestdistance();
void floyed();
void display(int i,int j);
/*主函数*/
void main()
{
int i,j;
char k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j]=INT_MAX;
cost[1][2]=cost[2][1]=80;
cost[1][6]=cost[6][1]=130;
cost[1][8]=cost[8][1]=130;
cost[2][3]=cost[3][2]=80;
cost[2][5]=cost[5][2]=120;
cost[2][4]=cost[4][2]=50;
cost[3][4]=cost[4][3]=70;
cost[3][5]=cost[5][3]=70;
cost[3][7]=cost[7][3]=100;
cost[4][6]=cost[6][4]=60;
cost[4][5]=cost[5][4]=110;
cost[4][7]=cost[7][4]=60;
cost[5][6]=cost[6][5]=130;
cost[5][7]=cost[7][5]=60;
cost[6][8]=cost[8][6]=60;
cost[6][7]=cost[7][6]=100;
cost[6][9]=cost[9][6]=140;
cost[7][9]=cost[9][7]=70;
cost[8][9]=cost[9][8]=150;
cost[1][1]=cost[2][2]=cost[3][3]=cost[4][4]=cost[5][5]=0;
cost[6][6]=cost[7][7]=cost[8][8]=cost[9][9]=0;
jiemian();
}
jiemian() /*主界面*/
{ int x=190,y=270,x1=200,y1=280,i=2;
int gdriver=9,gmode=VGAHI;
initgraph(&gdriver,&gmode,"C:\\TURBOC2");
cleardevice();
setbkcolor(CYAN);
settextstyle(0,0,1);
outtextxy(130,80,"WELCOME TO NORTHEAST DIANLI UNIVERSITY");
settextstyle(0,0,1);
outtextxy(190,240," Introduce the positions of the school.");
outtextxy(190,270," Show the shortest way of two positions.");
outtextxy(190,300," Quit.");
drt(x,y,x1,y1);
setcolor(BLUE);
settextstyle(0,0,2);
outtextxy(1,10,"NEDU");
settextstyle(0,0,1);
while(1)
{switch(getch())
{case 27:exit(1);
case 72:if(y>=270&&y<=330)
{drt1(x,y,x1,y1);y=y-30;y1=y1-30 ;drt(x,y,x1,y1); i=i-1;}break;
case 80:if(y1>=240&&y1<310)
{drt1(x,y,x1,y1);y=y+30;y1=y1+30;drt(x,y,x1,y1); i=i+1;}break;
case 13:a=i;switch(a)
{ case 1:{closegraph();
introduce();} break;
case 2:{fangzhen();
}break;
case 3: exit(0);
}
}
}
}
drt(int a,int b,int a1,int b1)
{setcolor(RED);
rectangle(a,b,a1,b1);
setfillstyle(SOLID_FILL,RED);
floodfill(a+1,b+1,RED);
}
drt1(int a,int b,int a1,int b1)
{setcolor(CYAN);
rectangle(a,b,a1,b1);
setfillstyle(SOLID_FILL,CYAN);
floodfill(a+1,b+1,CYAN);
}
fangzhen()
{ char i,j;
int e;
int gdriver=9,gmode=VGAHI;
initgraph(&gdriver,&gmode,"C:\\TURBOC2");
cleardevice();
setbkcolor(CYAN);
line(120,0,120,400);
line(120,400,270,400);
line(270,400,340,360);
line(340,360,440,360);
line(440,360,440,80);
line(440,80,120,80);
line(120,160,440,160);
line(120,320,440,320);
line(270,400,270,320);
line(310,160,310,80);
line(310,120,440,120);
line(350,80,350,0);
line(20,150,120,150);
line(20,150,20,220);
line(20,260,20,400);
line(20,320,120,320);
line(20,400,120,400);
setcolor(BLUE);
settextstyle(0,0,2);
outtextxy(1,10,"NEDU");
settextstyle(0,0,1);
setcolor(LIGHTGRAY);
rectangle(175,300,215,320);
rectangle(175,160,215,180);
rectangle(300,210,420,270);
setfillstyle(SOLID_FILL,LIGHTGRAY);
floodfill(176,301,LIGHTGRAY);
floodfill(176,161,LIGHTGRAY);
floodfill(301,211,LIGHTGRAY);
setcolor(WHITE);
rectangle(85,195,120,285);
rectangle(120,100,140,130);
rectangle(440,200,480,280);
rectangle(440,110,460,140);
rectangle(440,310,460,340);
rectangle(120,330,140,360);
rectangle(340,360,420,390);
rectangle(120,400,140,420);
rectangle(270,320,350,350);
setfillstyle(SOLID_FILL,WHITE);
floodfill(91,201,WHITE);
floodfill(121,101,WHITE);
floodfill(451,201,WHITE);
floodfill(451,121,WHITE);
floodfill(451,321,WHITE);
floodfill(121,331,WHITE);
floodfill(341,361,WHITE);
floodfill(121,401,WHITE);
floodfill(271,321,WHITE);
setcolor(LIGHTGREEN);
rectangle(220,95,300,145);
rectangle(270,280,420,315);
circle(250,360,15);
setfillstyle(SOLID_FILL,LIGHTGREEN);
floodfill(221,96,LIGHTGREEN);
floodfill(271,281,LIGHTGREEN);
floodfill(250,360,LIGHTGREEN);
setcolor(RED);
circle(120,400,2);
setfillstyle(SOLID_FILL,RED);
floodfill(120,400,RED);
outtextxy(121,410,"1.Dongjie");
circle(120,340,2);
setfillstyle(SOLID_FILL,RED);
floodfill(120,340,RED);
outtextxy(125,340,"2.Chan ye lou");
circle(120,240,2);
setfillstyle(SOLID_FILL,RED);
floodfill(120,240,RED);
outtextxy(100,245,"3.Zhulou");
circle(195,320,2);
setfillstyle(SOLID_FILL,RED);
floodfill(195,320,RED);
outtextxy(195,305,"4.Dong shi");
circle(195,160,2);
setfillstyle(SOLID_FILL,RED);
floodfill(195,160,RED);
outtextxy(195,170,"5.Xi shi");
circle(270,320,2);
setfillstyle(SOLID_FILL,RED);
floodfill(270,320,RED);
outtextxy(271,330,"6.Tushuguan");
circle(300,240,2);
setfillstyle(SOLID_FILL,RED);
floodfill(300,240,RED);
outtextxy(305,250,"7.Zhu shi tou lou");
circle(340,360,2);
setfillstyle(SOLID_FILL,RED);
floodfill(340,360,RED);
outtextxy(341,370,"8.Tiyuguan");
circle(440,240,2);
setfillstyle(SOLID_FILL,RED);
floodfill(440,240,RED);
outtextxy(442,225,"9.Er jiao");
outtextxy(110,110,"Zhonghe lou");
outtextxy(370,125,"Shu li lou");
outtextxy(443,320,"Xin xi hua zhong xin");
outtextxy(225,110,"Yuan lin");
outtextxy(290,300,"Yuan lin");
outtextxy(30,50,"Cao chang");
outtextxy(140,40,"Cao chang");
outtextxy(315,150,"Lanqiu ground");
outtextxy(315,110,"Wangqiu ground");
outtextxy(25,230,"Gate");
setcolor(WHITE);
rectangle(470,0,640,190);
setfillstyle(SOLID_FILL,GREEN);
floodfill(471,1,WHITE);
outtextxy(475,10,"Input the code:");
outtextxy(480,60,"Lu jing:");
outtextxy(475,120,"shortestdistance is:");
outtextxy(475,20,"From:");
line(518,30,560,30);
outtextxy(570,20,"To:");
line(597,30,635,30);
i=getch();
if(i<49||i>58)
{outtextxy(520,20,"Error");
getch();
fangzhen();} /*输入起点*/
sprintf(s,"%c",i);
outtextxy(530,20,s);
j=getch();
if(j<49||j>58)
{outtextxy(600,20,"Error");
getch();
fangzhen();} /*输入终点*/
sprintf(s,"%c",j);
outtextxy(610,20,s);
d=i-48;
tuse();
d=j-48;
tuse();
setcolor(WHITE);
shortestdistance(i,j);
setcolor(WHITE);
d=shortest[i-48][j-48];
sprintf(s,"%d",d);
outtextxy(475,130,s);
d=getch();
if(d==27)
jiemian();
else
fangzhen();
}
tuse() /*将景点颜色由红色变为绿色*/
{ setcolor(GREEN);
switch(d)
{case 1:circle(120,400,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(120,400,GREEN);break;
case 2:circle(120,340,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(120,340,GREEN);break;
case 3:circle(120,240,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(120,240,GREEN);break;
case 4:circle(195,320,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(195,320,GREEN);break;
case 5:circle(195,160,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(195,160,GREEN);break;
case 6:circle(270,320,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(270,320,GREEN);break;
case 7:circle(300,240,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(300,240,GREEN);break;
case 8:circle(340,360,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(340,360,GREEN);break;
case 9:circle(440,240,2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(440,240,GREEN);break;
}
}
int shortestdistance(int i,int j) /*要查找的两景点的最短距离*/
{
floyed();
display(i,j);
}/*shortestdistance*/
void floyed() /*用floyed算法求两个景点的最短路径*/
{int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{cost[j][i]=cost[i][j];
shortest[i][j]=cost[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