You are on page 1of 30

/*Code by: Hondacodon*/

Thut ton LC-BFS(Least Cost -Breadth First Search=tm kim chi ph t nht theo chiu Rng)
trc tin bn phi to 1 file input.txt trong a C:/

#include "stdafx.h" #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 100

typedef struct Queue//khai bao' hang doi { int *QArray; int QMax; int QNumItems; int QFront; int QRear; }QUEUE;

int InitQueue(QUEUE &q,int MaxItems)//khoi tao Hang` doi Rong { q.QArray=new int [MaxItems]; if(q.QArray==NULL) return 0; //het bo nho de cap phat'

/*Code by: Hondacodon*/

q.QMax=MaxItems; q.QNumItems=0; q.QFront=q.QRear= -1; //-1 tuc' la` chua co phan tu nao trong Quenue return 1; } int IsQueueEmpty(const QUEUE &q)//kiem tra hang doi co rong hay ko { if(q.QNumItems== -1) { return 1; // Queue Rong } return 0; //Queue Ko Rong } //khoi tao thanh cong

//Kiem tra Quenue da^y` int IsQueueFull(const QUEUE &q)//kiem tra hang` doi co' day` hay chua { if(q.QNumItems==q.QMax) { return 1; } return 0; } //Queue chua day //Queue day

int DeQueue(QUEUE &q , int newitem)//lay 1 pt o dau Queue,co the lam Queue thay doi

/*Code by: Hondacodon*/

{ if(IsQueueFull(q)) { return 0; } q.QRear++; if(q.QRear==q.QMax) { q.QRear=0; } q.QArray[q.QRear]=newitem; if(IsQueueEmpty(q)) q.QFront=0; q.QNumItems++; return 1;// them thanh cong } //Quenue day ko them dc

int EnQueue(QUEUE &q , int &outitem)//them 1 pt o cuoi Queue,co the lam Queue thay doi if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QFront]; q.QFront++; q.QNumItems--; if(q.QFront==q.QMax) q.QFront=0; if(IsQueueEmpty(q)) q.QFront=q.QRear=-1;

/*Code by: Hondacodon*/

return 1; }

//kiem tra 1 phan tu o dau Quenue, ko lam thay doi Queue int QueueFront(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; //Queue rong ,ko lay ra dc

outitem=q.QArray[q.QFront];//lay pt dau ra return 1; }

//kiem tra 1 phan tu o CuoiQuenue, ko lam thay doi Queue int QueueRear(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QRear];//lay pt o cuoi ra return 1; }

struct MYGRAPH { int N; int A[MAX][MAX]; int Previous[MAX]; };

/*Code by: Hondacodon*/

void InputGraph(MYGRAPH &g,char *file,int &start ,int &end) { int b=0; FILE*f= fopen(file,"r"); fscanf(f,"%d",&g.N); fscanf(f,"%d",&start); fscanf(f,"%d",&end); int i=0,j=0; while(!feof(f)) { if(b%g.N==0 && b!=0) { i++; j=0; } fscanf(f,"%d",&g.A[i][j]); j++; b++; } for(i=0;i<g.N;i++) { g.Previous[i]=-1; } fclose(f); } int BFS(MYGRAPH &g,int start,int end)

/*Code by: Hondacodon*/

{ int k[MAX]; QUEUE q; InitQueue(q,100); int flag=0; DeQueue(q,start); k[start]=0; int s; g.Previous[start] = -2; while(IsQueueEmpty(q)==0) { PushQueue(q,s); for(int i=0;i<g.N;i++) { if((g.A[s][i]!=0 && g.Previous[i]==-1) || (g.A[s][i]!=0 && k[s] +g.A[s][i]<k[i])) { PushQueue(q,i); g.Previous[i]=s; k[i]=k[s]+g.A [s][i]; } } } if (g.Previous [end]==-1) return 0; else return 1;

/*Code by: Hondacodon*/

} void PrintPath(MYGRAPH g,int end) { int i=end; printf("\nDuong di ngan nhat cua thuat toan BFS la`:\n"); printf("%d",end); i=g.Previous[i];

while(i!=-2) { printf("<-- %d",i); i=g.Previous[i]; } }

void main() {

MYGRAPH g; int start,end; InputGraph(g,"C:/input.txt",start ,end); printf("tim` duong di ngan' nhat Tu %d --> %d\n",start,end); if(BFS(g,start,end)) {

/*Code by: Hondacodon*/

PrintPath(g,end); } printf("\n"); getch(); }

Thut Ton BFS


LC BFS l thut ton nng cp cua BFS to 1 file input.txt nhu thaut toan LCBFS
// BFS.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include<stdio.h> #include<conio.h> #include<string.h> #define MAX 100

typedef struct Queue { int *QArray; int QMax; int QNumItems; int QFront; int QRear; }QUEUE;

/*Code by: Hondacodon*/

int InitQueue(QUEUE &q,int MaxItems) { q.QArray=new int [MaxItems]; if(q.QArray==NULL) return 0; q.QMax=MaxItems; q.QNumItems=0; q.QFront=q.QRear=-1; return 1; } int IsQueueEmpty(const QUEUE &q) { if(q.QNumItems==0) { return 1; } return 0; }

int IsQueueFull(const QUEUE &q) { if(q.QNumItems==q.QMax) { return 1; } return 0;

/*Code by: Hondacodon*/

10

int EnQueue(QUEUE &q , int newitem) { if(IsQueueFull(q)) { return 0; } q.QRear++; if(q.QRear==q.QMax) { q.QRear=0; } q.QArray[q.QRear]=newitem; if(IsQueueEmpty(q)) q.QFront=0; q.QNumItems++; return 1; }

int DeQueue(QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QFront]; q.QFront++; q.QNumItems--;

/*Code by: Hondacodon*/

11

if(q.QFront==q.QMax) q.QFront=0; if(IsQueueEmpty(q)) q.QFront=q.QRear=-1; return 1; }

int QueueFront(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QFront]; return 1; } int QueueRear(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QRear]; return 1; }

struct MYGRAPH { int N; int A[MAX][MAX]; int Previous[MAX];

/*Code by: Hondacodon*/

12

}; void InputGraph(MYGRAPH &g,char *file,int &start ,int &end) { int b=0; FILE*f= fopen(file,"r"); fscanf(f,"%d",&g.N); fscanf(f,"%d",&start); fscanf(f,"%d",&end); int i=0,j=0; while(!feof(f)) { if(b%g.N==0 && b!=0) { i++; j=0; } fscanf(f,"%d",&g.A[i][j]); j++; b++; } for(i=0;i<g.N;i++) { g.Previous[i]=-1; } fclose(f); } int BFS(MYGRAPH &g,int start,int end)

/*Code by: Hondacodon*/

13

{ QUEUE q; InitQueue(q,100); int flag=0; EnQueue(q,start); int s; g.Previous[start] = -2; while(IsQueueEmpty(q)==0 && flag==0) { DeQueue(q,s); for(int i=0;i<g.N;i++) { if(g.A[s][i]!=0 && g.Previous[i]==-1) { EnQueue(q,i); g.Previous[i]=s; if(i==end) { flag=1; return flag; } } } } return flag; } void PrintPath(MYGRAPH g,int end)

/*Code by: Hondacodon*/

14

{ int i=end; printf("\nDuong di ngan nhat la:\t"); printf("%d",end); i=g.Previous[i];

while(i!=-2) { printf("<- %d",i); i=g.Previous[i]; } }

void main() {

MYGRAPH g; int start,end; InputGraph(g,"C:/input.txt",start ,end); printf("duong di BFS duyet theo chieu rong tu:%d --> %d \n",start,end); if(BFS(g,start,end)) { PrintPath(g,end); }

/*Code by: Hondacodon*/

15

printf("\n\n"); }

Thut Ton UCS


// UCS.cpp : Defines the entry point for the console application. //#include "stdafx.h" #include <conio.h> #include <stdio.h> #include <stdlib.h> typedef struct map { int n; int a[100][100]; int ChiPhi[100]; int DaXet[100]; int LuuVet[100]; }; typedef struct map DoThi; //cac ham dc khai bao //void Doc_Mang(DoThi &d,char *fn,int &start,int &end) ; //void Khoi_Tao(DoThi &d); //void ThanhDong_UCS(DoThi &d,int start,int end); int Path_ThanhDong_UCS(DoThi d,char *fn,int start,int end); //---------------------------------------------------//

//ham void main() //------------------------------------------//

//ham doc file input.txt void Doc_Mang(DoThi &d,char *fn,int &start,int &end) { FILE *f=fopen(fn,"r"); int i,j; if (f!= NULL) { fscanf (f,"%d",&d.n); fscanf(f,"%d %d",&start,&end); for (i=0;i<d.n;i++) { for (j=0;j<d.n;j++) {

/*Code by: Hondacodon*/

16

fscanf (f,"%d",&d.a[i][j]); if(d.a[i][j]==0) d.a[i][j]=3000; } } printf("\nMo File Thanh Cong"); } else { printf ("Loi Mo File !! "); } fclose(f); } //-----------------------------------//

//ham khoi tao void Khoi_Tao(DoThi &d) { int i; for (i=0;i<d.n;i++) { d.ChiPhi[i] = 0; d.DaXet[i] = 0; d.LuuVet[i] = 0; } } //---------------------//

//ham thuat toan UCS void ThanhDong_UCS(DoThi &d,int start,int end) { int u,i; int min; d.ChiPhi[start]=0;d.DaXet[start]=1;d.LuuVet[start]=1; while (!d.DaXet[end]) { min=3000; for (i=0;i<d.n;i++) { if((!d.DaXet[i])&& (min > d.ChiPhi[i])) { min = d.ChiPhi[i]; u = i; } } d.DaXet[u]=1; if (!d.DaXet[end])

/*Code by: Hondacodon*/

17

{ for (i=0;i<d.n;i++) { if ((!d.DaXet[i]) && (d.ChiPhi[i] > (d.ChiPhi[u] + d.a[u][i]))) { d.ChiPhi[i] = d.ChiPhi[u] + d.a[u][i]; d.LuuVet[i] = u; } } } } } //-----------------------------------------------------------------------//

//ham tim tim duong dj tren thuat toan UCS int Path_ThanhDong_UCS(DoThi d,char*fn,int start,int end) { int i,khong=0; FILE *f=fopen(fn,"wt"); if(f==NULL) return 0; for (i=0;i<d.n;i++) { d.ChiPhi[i]=d.a[start][i]; d.LuuVet[i] = start; d.DaXet[i]=0; } ThanhDong_UCS(d,start,end); if(d.DaXet[end]==1) { if(start==end) { fprintf(f,"%d\n",khong); fprintf(f,"%d",start); } else { fprintf(f,"%d\n",d.ChiPhi[end]); fprintf(f,"%d ",end); i=d.LuuVet[end]; while (i!=start) { fprintf(f,"%d ",i); i=d.LuuVet[i]; } fprintf(f,"%d ",start); } } else

/*Code by: Hondacodon*/

18

fprintf(f,"0"); return 1; fclose(f); } int main() { DoThi d;int start,end; Doc_Mang(d,"input.txt",start,end); Khoi_Tao(d); ThanhDong_UCS(d,start,end); Path_ThanhDong_UCS(d,"C:/UCS_input.txt",start,end); return 0; getch(); }

Thut Ton DFS


/*file ma tran: 6 1 0 3 8 1 2 */ #include <iostream.h> #include <conio.h> #define MAX 100 struct Graph{ int n; int data[MAX][MAX]; }; struct stack{ int data; stack *next; }; typedef stack *pstack; struct node{ int data; node *next; };

2 0 3 2 2 2

3 1 4 0 1 2

4 1 4 1 1 0

5 2 5 2 1 0

6 2 6 3 3 0

/*Code by: Hondacodon*/

19

typedef node *pnode; void createStack(pstack &S); void insertTop(pstack &S, int x); void getTop(pstack &S, int &t); void insert(pnode &H, int x); void outputMatrix(Graph A); void output(pnode H); void readFile(char *path, Graph &A); int isScalar(Graph A);//1: Yes - 0: No int calculateOrderTip(Graph A, int o); int isVisited(pnode C, int t);//1: Yes - 0: No void findNextDoor(int Tip, Graph A, pnode &H); void DFS(Graph A); int main() { int x; char path[MAX]; Graph A; cout<<"Nhap duong dan tap tin: "; gets(path); readFile(path,A); cout<<"Matran vua nhap: "<<"\n"; outputMatrix(A); cout<<"Nhap dinh can tinh bac: "; cin>>x; cout<<"Bac cua dinh la: "<<calculateOrderTip(A,x); cout<<"\nDuyet Do thi theo DFS: "; DFS(A); getch(); return EXIT_SUCCESS; } void readFile(char * path, Graph &A) { FILE *f; int i,j; f=fopen(path,"rt"); if (f == NULL) { cout<<"Loi mo file!"; return; } fscanf(f,"%d ",&A.n); while (!feof(f)) { for (i=0; i<A.n; i++) { for (j=0; j<A.n; j++)

/*Code by: Hondacodon*/

20

{ fscanf(f,"%d ",&A.data[i][j]); } } } fclose(f); } void createStack(pstack &S) { S= NULL; } void insert(pnode &H, int x) { pnode p; if (H==NULL) { p=new node; p->data=x; p->next=H; H=p; } else { p=H; while (p->next!=NULL) p=p->next; p->next=new node; p=p->next; p->data = x; p->next=NULL; } } void insertTop(pstack &S, int x) { pstack p; p=new stack; p->data=x; p->next=S; S=p; } void getTop(pstack &S, int &t) { pstack p; if (S== NULL) { cout<<"Stack is empty!"; } else

/*Code by: Hondacodon*/

21

{ t=S->data; p=S; S=S->next; delete p; } } void outputMatrix(Graph A) { int i,j; for (i=0; i<A.n; i++) { for (j=0; j<A.n; j++) { cout<<A.data[i][j]<<" "; } cout<<"\n"; } } void output(pnode H) { pnode p=H; if (H==NULL) cout<<"Danh sach rong!"; else { while (p!= NULL) { cout<<p->data<<" "; p=p->next; } } } int isScalar(Graph A) { int i,j; for (i=0; i<A.n; i++) { for (j=0; j<A.n; j++) { if (A.data[i][j] != A.data[j][i]) { return false; } } } return true; }

/*Code by: Hondacodon*/

22

int calculateOrderTip(Graph A,int o) { int i; int S=0; if (isScalar(A)) { for (i=0; i<A.n; i++) { S+=A.data[o][i]; } } else { for (i=0; i<A.n; i++) { S+=A.data[o][i]; } for (i=0; i<A.n; i++) { S+=A.data[i][o]; } } return S; } int isVisited(pnode C, int t) { pnode p=C; while (p!=NULL) { if (p->data == t) { return true; } p=p->next; } return false; } void findNextDoor(int Tip, Graph A, pnode &H) { int i; for (i=0; i<A.n;i++) { if (A.data[Tip][i] > 0) { insert(H,i); } } }

/*Code by: Hondacodon*/

23

void DFS(Graph A) { pnode C=NULL; int i=0;int j; int temp; pnode H; pnode p; pstack O; createStack(O); do { for (j=0; j<A.n;j++) { if (!isVisited(C,j)) { insertTop(O,j); break; } } while (O != NULL) { H=NULL; getTop(O,temp); if (!isVisited(C,temp)) { insert(C,temp); i++; findNextDoor(temp,A,H);//Finf all Tip next-door to the current Tip //Put Tip into O p=H; while (p!=NULL) { insertTop(O,p->data); p=p->next; } } } }while (i<=A.n-1); //Output DFS output(C); }

Thut Ton A*(A start)

/*Code by: Hondacodon*/

24

/* file:input.txt 6 4 1 3 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 */ #include #include #include #include #include "stdafx.h" <stdio.h> <conio.h> <math.h> "stdlib.h"

struct vitri { int x; int y; }; typedef struct trangthai { vitri vector; int g; int f; int huong; vitri Pre; }TT; struct QUEUE { TT *mang; int max; int size; int start; int end; }; TT dong[100][100]; struct dothi { int n; int m; int **a; };

/*Code by: Hondacodon*/

25

int nhapfile(dothi &l,char *fn,TT &start,TT &end) { FILE*f=fopen(fn,"rt"); if(f==NULL) { return 0;//printf("\nMO File Bi Loi!\n"); } else { fscanf(f,"%d %d",&start.vector.x,&start.vector.y); fscanf(f,"%d %d",&end.vector.x,&end.vector.y); fscanf(f,"%d %d",&l.m,&l.n); l.a=new int*[l.m]; for(int i=0;i<l.m;i++) { l.a[i]=new int[l.n]; for(int j=0;j<l.n;j++) { fscanf(f,"%4d",&l.a[i][j]); printf("%4d",l.a[i][j]); dong[i][j].huong=-2; } printf("\n"); } } fclose(f); return 1; } int khoitao_Queue(QUEUE &Q,int max) { Q.mang=new TT[max]; if(Q.mang==NULL) return 0; Q.max=max; Q.size=0; Q.start=Q.end=-1; return 1; } int kiemtraRong(const QUEUE &Q) { if(Q.size==0) return 1; return 0; } int kiemtraDay(const QUEUE &Q) { if(Q.size>=Q.max) return 1; return 0; }

/*Code by: Hondacodon*/

26

int dua_vao_hangdoi(QUEUE &Q,TT &x) { if(kiemtraDay(Q)) return 0; Q.end++; if(Q.end==Q.max) Q.end=0; Q.mang[Q.end]=x; if(kiemtraRong(Q)) Q.start=0; Q.size++; return 1; } int lay_giatri(QUEUE &Q,TT &value) { if(kiemtraRong(Q)) return 0; int min=Q.start; int timkiemdiachi=0; int i=Q.start+1; for(;i<Q.size;i++) { if(Q.mang[i].f<Q.mang[min].f) min=i; if(i==Q.end) { timkiemdiachi=1; break; } } if(timkiemdiachi=0) { for(int j=0;j<Q.end;j++) if(Q.mang[j].f<Q.mang[min].f) min=j; } value=Q.mang[min]; Q.mang[min]=Q.mang[Q.end]; Q.size--; if(kiemtraRong(Q)) Q.start=Q.end=-1; else { Q.end--; if(Q.end<0) Q.end=Q.max-1; } return 1; } int h(vitri u, vitri v)

/*Code by: Hondacodon*/

27

{ int kq; int x=abs(u.x -v.x); int y=abs(u.y -v.y); kq=(x +y); return kq; } int sosanh2vectoc(vitri u,vitri v) { if(u.x == v.x && u.x == v.x) return 1; return 0; } TT day(vitri dau,TT v,vitri cuoi,int m) { TT kq; kq.vector.x= dau.x; kq.vector.y= dau.y; kq.g= v.g + 1; kq.f = kq.g + h(kq.vector,cuoi); kq.huong=m; kq.Pre.x= v.vector.x; kq.vector.y= v.vector.y; return kq; } int AStar(dothi d,TT dau,TT cuoi) { if(sosanh2vectoc(dau.vector,cuoi.vector)) return 1; QUEUE Q; int k=d.m+d.n; khoitao_Queue(Q,k); dau.g=0; dau.f=h(dau.vector,cuoi.vector); dau.huong=-1; dua_vao_hangdoi(Q,dau); while(!kiemtraRong(Q)) { TT k; lay_giatri(Q,k); if(sosanh2vectoc(k.vector,cuoi.vector)) { dong[k.vector.x][k.vector.y].Pre=k.Pre; dong[k.vector.x][k.vector.y].huong=k.huong; dong[k.vector.x][k.vector.y].g=k.g; return 1; } if(dong[k.vector.x][k.vector.y].huong<0) { dong[k.vector.x][k.vector.y].Pre = k.Pre; dong[k.vector.x][k.vector.y].huong = k.huong;

/*Code by: Hondacodon*/

28

vitri u; u.x = k.vector.x - 1; u.y= k.vector.y; if(u.x >= 0) { if(d.a[u.x][u.y] == 0) { if(dong[u.x][u.y].huong == -2) { TT t = day(u,k,cuoi.vector,0); dua_vao_hangdoi(Q,t); } } } u.x = k.vector.x; u.y= k.vector.y+ 1; if(u.y<d.n) { if( d.a[u.x][u.y] == 0) { if(dong[u.x][u.y].huong == -2) { TT t = day(u,k,cuoi.vector,1); dua_vao_hangdoi(Q,t); } } } u.x = k.vector.x+1; u.y= k.vector.y; if(u.x < d.m ) { if( d.a[u.x][u.y] == 0) { if(dong[u.x][u.y].huong == -2) { TT t = day(u,k,cuoi.vector,2); dua_vao_hangdoi(Q,t); } } } u.x = k.vector.x; u.y = k.vector.y-1; if(u.y >= 0) { if(d.a[u.x][u.y] == 0) {

/*Code by: Hondacodon*/

29

if(dong[u.x][u.y].huong == -2) { TT t = day(u,k,cuoi.vector,3); dua_vao_hangdoi(Q,t); } } } } } return 0; } int Output(TT end,int kq,char *fn) { FILE *f = fopen(fn,"wt"); if(f==NULL) return 0; else { if(kq) { if(dong[end.vector.x][end.vector.y].g == 0) { fprintf(f,"0"); fclose(f); exit(1); } int duongdi[100]; int dodai = 0; duongdi[dodai++] = dong[end.vector.x][end.vector.y].huong; vitri prev = dong[end.vector.x][end.vector.y].Pre; while(dong[prev.x][prev.y].huong!= -1) { duongdi[dodai++] = dong[prev.x][prev.y].huong; prev = dong[prev.x][prev.y].Pre; } fprintf(f,"%d\n",dong[end.vector.x][end.vector.y].g); for(int j = dodai - 1; j >= 0; j--) fprintf(f,"%d ",duongdi[j]); } else fprintf(f,"-1"); fclose(f); }

/*Code by: Hondacodon*/

30

return 1; } int main(int argc, char* argv[]) { dothi d; TT start,end; int kq=AStar(d,start,end); if(nhapfile(d,"input.txt",start,end)==0) printf("\nMO File Bi Loi!\n"); else printf("MO file thanh cong!"); Output(end,kq,"0863031.txt"); return 0; }

You might also like