-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGhinhanDuongdi.cpp
63 lines (62 loc) · 1.34 KB
/
GhinhanDuongdi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <conio.h>
#include <iostream>
#define MAX 50
#define TRUE 1
#define FALSE 0
int A[MAX][MAX], n,chuaxet[MAX], truoc[MAX], s, t;
void Init(void){//Doc du lieu va khoi dau cac bien
int i,j;FILE *fp;
fp=fopen("dothi_duongdi.in","r");
fscanf(fp,"%d",&n);
printf("\n So dinh do thi:%d",n);
for(i=1; i<=n; i++){
printf("\n");chuaxet[i]=TRUE;truoc[i]=0;
for(j=1; j<=n; j++){
fscanf(fp,"%d",&A[i][j]);
printf("%3d",A[i][j]);
}
}
}
void DFS(int u){//Thuat toan DFS
int v;
printf("%3d",u);chuaxet[u]=FALSE;
for(v=1; v<=n; v++){
if(A[u][v] && chuaxet[v]){
truoc[v]=u;DFS(v);
}
}
}
void BFS(int i){//Thuat toan BFS
int queue[MAX], low=1, high=1, u, v;
queue[low]=i;chuaxet[i]=FALSE;
while(low<=high){
u = queue[low];low=low+1;
for(v=1; v<=n; v++){
if(A[u][v] && chuaxet[v]){
high = high+1;queue[high]=v;
truoc[v]=u; chuaxet[v]=FALSE;
}
}
}
}
void Duongdi (void){
if (truoc[t]==0){
printf("\n Khong ton tai duong di");
getch(); return;
}
printf("\n Duong di:");
int j = t;printf("%3d<=",j);
while(truoc[j]!=s){
printf("%3d<=",truoc[j]);j=truoc[j];
}
printf("%3d",s); getch();
}
int main (void){
Init();
printf("\n Dinh dau: ");scanf("%d",&s);
printf("\n Dinh cuoi: ");scanf("%d",&t);
//DFS(s);
BFS(s);
Duongdi();
}