码迷,mamicode.com
首页 > 其他好文 > 详细

Artificial Intelligence

时间:2014-07-29 17:40:12      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   2014   

//**************************************BEST-FS ALRORITHM IN ARTIFICAL INTELLIGENCE**************************************

// Created by Clivia_zhou . 2014.7.29
#include <iostream>
using namespace std;

//define functions
//Some function has‘n finshied
typedef 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-search
void 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 step
void 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 ;
}

 

Artificial Intelligence,布布扣,bubuko.com

Artificial Intelligence

标签:des   style   blog   color   os   io   for   2014   

原文地址:http://www.cnblogs.com/cliviazhou/p/3875814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!