八数码c语言编程深度搜索,图算法_普通广度优先搜索(BFS)解八数码问题_C语言

Copyright (C)2009 Chris Xue.

Permission is granted to copy, distribute and/or modify this

document under the terms of the GNU Free Documentation License,

Version 1.3 or any later version published by the Free Software

Foundation; with no Invariant Sections, no Front-Cover Texts and

no Back-Cover Texts. A copy of the license is included in the

section entitled "GNU Free Documentation License".

//main.c

#include "head.h"

#include

int main(int argc, char **argv)

{

int initial[TOTAL],goal[TOTAL];

int i;

if( argc!=2 || freopen(argv[1],"r",stdin)==NULL)

return 0;

printf("Initial:/n");

for(i=0;i

{

scanf("%d",&initial[i]);

}

printf("/nGoal:/n");

for(i=0;i

{

scanf("%d",&goal[i]);

}

BFSSolve(initial,goal);

return 0;

}

//head.h

#include

#define COL 3

#define ROW 3

#define TOTAL 9

struct STATE

{

int matrix[TOTAL];

struct STATE *parent;

};

typedef struct STATE State;

struct QUEUE

{

struct STATE *current;

struct QUEUE *next;

};

typedef struct QUEUE Queue;

int BFSSolve(const int *, const int *);

int EnqueueChild(const State *);

int MoveAndEnqueue(const State *, const int, const int );

State *StateConstruct(const int *, const State *);

Queue *QueueConstruct(const State *);

int IsEqual(const int *, const int *);

int DequeueOpenEnqueueClosed();

int ShowSolutionPath(const State *);

int Destruct();

int openDequeue();

int closedDequeue();

//function.c

#include "head.h"

#include

Queue *openHead = NULL;

Queue *openTail = NULL;

Queue *closedHead = NULL;

Queue *closedTail = NULL;

int BFSSolve(const int *entryArray, const int *goal)

{

State *entryState = StateConstruct(entryArray,NULL);

openHead = QueueConstruct(entryState);

openTail = openHead;

while( ! IsEqual(openHead->current->matrix,goal) )

{

EnqueueChild(openHead->current);

DequeueOpenEnqueueClosed();

}

ShowSolutionPath(openHead->current);

Destruct();

return 1;

} /*end BFSSolve()*/

int EnqueueChild(const State *parent)

{

int i;

for(i=0;i

{

if(parent->matrix[i]==0)

{

if( (i+1)/COL == i/COL )

MoveAndEnqueue(parent,i,i+1);

if( (i-1+COL)/COL == (i+COL)/COL ) /*excluding 0-1*/

MoveAndEnqueue(parent,i,i-1);

if( i+COL < TOTAL )

MoveAndEnqueue(parent,i,i+COL);

if( i-COL >= 0 )

MoveAndEnqueue(parent,i,i-COL);

break;

}

}

return 0;

}

int MoveAndEnqueue(const State *parent, const int x, const int y)

{

State *state = StateConstruct(parent->matrix,parent);

Queue *queue = QueueConstruct(state);

state->matrix[x] = parent->matrix[y];

state->matrix[y] = parent->matrix[x];

openTail->next = queue;

openTail = queue;

return 0;

} /*end MoveAndEnqueue()*/

State *StateConstruct(const int *matrix, const State *parent)

{

int i;

State *state = (State*)malloc(sizeof(State));

if( state==NULL )

{

Destruct();

exit(0);

}

for(i=0;i

state->matrix[i] = matrix[i];

state->parent = parent;

return state;

} /*end StateConstruct()*/

Queue *QueueConstruct(const State *state)

{

Queue *queue = (Queue*)malloc(sizeof(Queue));

if( queue==NULL )

{

Destruct();

exit(0);

}

queue->current = state;

queue->next = NULL;

return queue;

} /*end QueueConstruct()*/

int IsEqual(const int *matrix, const int *goal)

{

int i;

for(i=0;i

if(matrix[i] != goal[i])

return 0;

return 1;

} /*end IsEqual()*/

int DequeueOpenEnqueueClosed()

{

if(closedHead == NULL)

{

closedHead = openHead;

closedTail = openHead;

openHead = openHead->next;

closedTail->next = NULL;

}

else

{

closedTail->next = openHead;

closedTail = openHead;

openHead = openHead->next;

closedTail->next = NULL;

}

return 0;

} /*end DequeueOpenEnqueueClosed()*/

int ShowSolutionPath(const State *state)

{

State *curState = state;

Queue *curQueue = NULL;

Queue *lastQueue = NULL;

Queue *temp;

int i;

while(curState != NULL)

{

curQueue = (Queue*)malloc(sizeof(Queue));

if( curQueue==NULL )

{

while(curQueue != NULL)

{

temp = curQueue;

curQueue = curQueue->next;

free(temp);

}

Destruct();

exit(0);

}

curQueue->current = curState;

curQueue->next = lastQueue;

lastQueue = curQueue;

curState = curState->parent;

}

while(curQueue != NULL)

{

for(i=0;i

{

if( i%COL == 0) printf("/n");

printf("%d ",curQueue->current->matrix[i]);

}

temp = curQueue;

curQueue = curQueue->next;

free(temp);

printf("/n");

}

return 0;

}

int Destruct()

{

Queue *temp;

while(closedHead != NULL)

{

free(closedHead->current);

temp = closedHead;

closedHead = closedHead->next;

free(temp);

}

while(openHead != NULL)

{

free(openHead->current);

temp = openHead;

openHead = openHead->next;

free(temp);

}

return 0;

}

int openDequeue()

{

Queue *headTemp;

headTemp = openHead;

openHead = openHead->next;

free(headTemp->current);

free(headTemp);

return 0;

}

int closedDequeue()

{

Queue *headTemp;

headTemp = openHead;

openHead = openHead->next;

free(headTemp->current);

free(headTemp);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值