首页 > 代码库 > 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 ;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。