首页 > 代码库 > Artificial Intelligence

Artificial Intelligence

//**************************************BEST-FS ALRORITHM IN ARTIFICAL INTELLIGENCE**************************************// Created by Clivia_zhou . 2014.7.29#include <iostream>using namespace std;//define functions//Some function has‘n finshiedtypedef struct pqueue_t{}pqueue_t;typedef struct queue_t{}queue_t;typedef struct node_t{    int g;    int h;    unsigned short board;}node_t;void generateChildNodes(pqueue_t*,queue_t*,node_t*);void dePQueue(pqueue_t*,int*,int);bool isEmptyPQueue(pqueue_t*);void emitBoard(node_t*);bool checkPiece(unsigned short,int);bool searchQueue(queue_t*,unsigned short);void createNode(pqueue_t*,queue_t*,unsigned short,int);// The best_fs is the main function of best-first-searchvoid best_fs(pqueue_t *open_pq_p,queue_t* closed_q_p){    node_t *node_p;    int cost;        while(!isEmptyPQueue(open_pq_p))    {                dePQueue(open_pq_p,(int*)&node_p,cost);                if(node_p->g == 0)        {            printf("Found Solution(depth:%d):\n",node_p->h);            emitBoard(node_p);            break;        }                generateChildNodes(open_pq_p,closed_q_p,node_p);    }    return ;}//The generateChildNodes is the solution of every stepvoid generateChildNodes(pqueue_t *pq_p,queue_t *closed_q_p,node_t* node_p){    int i;        unsigned short cboard1,cboard2;        const int moves[16] = {-1,2,2,1,-1,2,2,1,-1,2,2,1,-1,2,2,1};        for(i = 0;i<16;i++)    {        if(checkPiece(node_p->board,i))        {            cboard1 = cboard2 = (node_p->board & ~(1<<(15 - i)));                        if(moves[i] == -1)            {                cboard1 |= (1<<(15-(i + 1)));                                if(!searchQueue(closed_q_p,cboard1))                {                    (void)createNode(pq_p,closed_q_p,cboard1,node_p->h+1);                }            }                        else if(moves[i] == 2)            {                cboard1 |= (1<<(15-(i + 1)));                                if(!searchQueue(closed_q_p,cboard1))                {                    (void)createNode(pq_p,closed_q_p,cboard1,node_p->h+1);                }                                cboard2 |= (1<<(15-(i - 1)));                                if(!searchQueue(closed_q_p,cboard2))                {                    (void)createNode(pq_p,closed_q_p,cboard2,node_p->h+1);                }                            }                        else if(moves[i] == 1)            {                cboard2 |= (1<<(15 - (i-1)));                                if(searchQueue(closed_q_p,cboard2))                {                    (void)createNode(pq_p,closed_q_p,cboard2,node_p->h+1);                }            }        }    }    return ;}