Tải bản đầy đủ (.pdf) (30 trang)

thuat toan AI-TTNT

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (491.29 KB, 30 trang )

/*Code by: Hondacodon*/
1


Thuật toán LC-BFS(Least Cost -Breadth First Search=tìm kiếm
chi phí ít nhất theo chiều Rộng)
trước tiên bạn phải tạo 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*/
2

q.QMax=MaxItems;

q.QNumItems=0;

q.QFront=q.QRear= -1; //-1 tuc' la` chua co phan tu
nao trong Quenue


return 1; //khoi tao thanh cong

}

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

}



//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; //Queue day

}

return 0; //Queue chua day

}



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

/*Code by: Hondacodon*/
3

{

if(IsQueueFull(q))

{

return 0; //Quenue day ko them dc

}


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

}



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*/
4


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*/
5


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*/
6

{

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*/
7


}

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*/
8


PrintPath(g,end);

}

printf("\n");

getch();

}

Thuật Toán BFS
LC BFS là thuật toán nâng cấp cua BFS
tạo 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*/
9




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");

}

Thuật Toán 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();
}

Thuật Toán DFS
/*file ma tran:

6
1 2 3 4 5 6
0 0 1 1 2 2
3 3 4 4 5 6
8 2 0 1 2 3
1 2 1 1 1 3
2 2 2 0 0 0

*/


#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;
};
/*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);
}


Thuật Toán 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 "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include "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;
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×