Anh chị cho em hỏi em viết chương trình THUẬT TOÁN BFS nhưng không biết sai chỗ nào, chỉ in được ra ma trận và số đỉnh, nhập u sau đó chương trình dừng luôn không chạy nữa. Em cảm ơn ạ.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define N 100
typedef struct queue{
int data[N];
int top;
}queue;
queue hd;
int A[N][N],daxet[N]; int n,u;
void docfile(){
int i,j;
FILE *f = fopen("matran.txt","r");
if( f == NULL){
printf("khong tim thay file");
exit(1);
}
else{
fscanf(f,"%d",&n);
for(i = 0; i<n; i++){
for(j = 0; j<n; j++){
fscanf(f,"%d",&A[i][j]);
}
daxet[i] = 0;
}
}
fclose(f);
}
void xuatfile(){
int i,j;
printf("so dinh cua do thi %d \n\n",n);
for(i = 0; i<n; i++){
for(j = 0; j<n; j++){
printf("%d ",A[i][j]);
}
printf("\n");
}
}
void push(int x){
hd.data[hd.top++] = x;
}
int pop(){
int i;
int result = hd.data[0];
for(i = 1; i< hd.top; i++){
hd.data[i-1] = hd.data[i] ;
hd.top--;
}
return result;
}
void BFS(){
int v,s;
(hd).top = 0;//xem lai
push(u);
daxet[u]= 1;
while(hd.data!=NULL){
s = hd.top;
pop();
printf("%d ",u+1);
for(v = 1; v <= n; v++){
if(A[s][v] == 1 && daxet[v] == 0){
daxet[v] = 1;
push(v);
}
}
}
}
int main(){
int u;
docfile();
xuatfile();
printf("nhap dinh u: \n"); scanf("%d",&u);
BFS();
getch();
return 0;
}


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?