Sie sind auf Seite 1von 5

PROGRAM DFS & BFS

PROYEK ROMANIA
#include<stdio.h>
#include<conio.h>
int bobot[20][20],child[20],visited[20],opened[20],closed[20],jumkota,i,j,f=0,r=-
1,c=0;
char kota[20][20];
char kotacari[20];
void bfs(int v,int y)
{
int i,j;
printf("\n\nX = %s",kota[v]);
printf("\nOpen :");
for(i=f;i<=r;i++)
printf("%s ",kota[child[i]]);

for(i=0;i<jumkota;i++)
if(bobot[v][i] && !visited[i] && !opened[i])
{
child[++r]=i;
opened[i]=1;
}

if (v==y)
printf("\n\n SOLUTION FOUND !");
else
{
printf("\nChildren :");
for(i=0;i<jumkota;i++)
if (bobot[v][i])
printf("%s ",kota[i]);

printf("\nClosed :");
closed[c++]=v;
for(i=0;i<c;i++)
printf("%s ",kota[closed[i]]);
if (f<=r)
{
visited[child[f]]=1;
bfs(child[f++],y);
}
}
}

void main()
{
int v,y;
char temp1[20], temp2[20];

jumkota=20;
printf("\n THE ROMANIA PROJECT\n\n");
strcpy(kota[0],"Arad");
strcpy(kota[1],"Zerind");
strcpy(kota[2],"Sibiu");
strcpy(kota[3],"Timisoara");
strcpy(kota[4],"Oradea");
strcpy(kota[5],"Lugoj");
strcpy(kota[6],"Fagaras");
strcpy(kota[7],"RimniciuVilcea");
strcpy(kota[8],"Pitesti");
strcpy(kota[9],"Craiova");
strcpy(kota[10],"Mehadia");
strcpy(kota[11],"Drobeta");
strcpy(kota[12],"Bucharest");
strcpy(kota[13],"Giurgiu");
strcpy(kota[14],"Urziceni");
strcpy(kota[15],"Hirsova");
strcpy(kota[16],"Eforie");
strcpy(kota[17],"Vaslui");
strcpy(kota[18],"Lasi");
strcpy(kota[19],"Neamt");

for(i=0;i<jumkota;i++)
{
child[i]=0;
visited[i]=0;
closed[i]=0;
opened[i]=0;
for(j=0;j<jumkota;j++)
{
bobot[i][j]=0;
}
}

bobot[0][1]=75; bobot[0][2]=140; bobot[0][3]=118;


bobot[1][0]=75; bobot[1][4]=71;
bobot[2][0]=140; bobot[2][4]=151; bobot[2][6]=99; bobot[2][7]=80;
bobot[3][0]=118; bobot[3][5]=111;
bobot[4][1]=71; bobot[4][2]=151;
bobot[5][3]=111; bobot[5][10]=70;
bobot[6][2]=99; bobot[6][12]=211;
bobot[7][2]=80; bobot[7][8]=97; bobot[7][9]=146;
bobot[8][7]=97; bobot[8][9]=138; bobot[8][12]=101;
bobot[9][7]=146; bobot[9][8]=138; bobot[9][11]=120;
bobot[10][5]=70; bobot[10][11]=75;
bobot[11][9]=120; bobot[11][10]=75;
bobot[12][6]=211; bobot[12][8]=101; bobot[12][13]=90; bobot[12][14]=85;
bobot[13][12]=90;
bobot[14][12]=85; bobot[14][15]=98; bobot[14][17]=142;
bobot[15][14]=98; bobot[15][16]=86;
bobot[16][15]=86;
bobot[17][14]=142; bobot[17][18]=92;
bobot[18][17]=92; bobot[18][19]=87;
bobot[19][18]=87;

for(i=0;i<jumkota;i++)
{
printf("%s -> ",kota[i]);
for(j=0;j<jumkota;j++)
if (bobot[i][j])
printf("%s (%d) ",kota[j],bobot[i][j]);
printf("\n");
}

printf("\n\nEnter a city to find:");


scanf("%s",&kotacari);
printf("\n\nSearching for %s....",kotacari);
v=0;
y=-1;
for(i=0;i<jumkota;i++)
{
for(j=0;j<strlen(kotacari);j++)
temp1[j] =tolower(kotacari[j]);
temp1[j] = '\0';
for(j=0;j<strlen(kota[i]);j++)
temp2[j] =tolower(kota[i][j]);
temp2[j] = '\0';
if (!strcmp(temp1,temp2))
y=i;
}

visited[v]=1;
bfs(v,y);

getch();
}

Das könnte Ihnen auch gefallen