回复 14楼 冰镇柠檬汁儿
你应该看看我 2011年1月 写的广度优先搜索
https://bbs.bccn.net/thread-331463-1-1.html
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 5
typedef struct bgMatrix
{
int v, w;
char matrix[NUM][NUM];
int pre;
}Matrix;
typedef struct bgQueue
{
Matrix* data;
int maxLength;
int head;
int tail;
}BGQueue;
typedef BGQueue* Queue;
typedef struct bgStack
{
Matrix* data;
int top;
}BGStack;
typedef BGStack* Stack;
char srcMatrix[NUM][NUM] =
{
{'*', '*', '*', '*', '*'},
{'*', '2', '8', '3', '*'},
{'*', '1', '0', '4', '*'},
{'*', '7', '6', '5', '*'},
{'*', '*', '*', '*', '*'}
};
char dstMatrix[NUM][NUM] =
{
{'*', '*', '*', '*', '*'},
{'*', '1', '2', '3', '*'},
{'*', '8', '0', '4', '*'},
{'*', '7', '6', '5', '*'},
{'*', '*', '*', '*', '*'}
};
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int cnt = -1;
Queue queue;
Stack stack;
FILE* log;
void bfs(Matrix matrix);
void initQueue(int length);
void putQueue(Matrix matrix);
Matrix getQueue(void);
int isQueueEmpty(void);
int isQueueFull(void);
void initStack(int length);
void pushStack(Matrix matrix);
Matrix popStack(void);
int isStackEmpty(void);
int matrixCmp(char src[][NUM], char dst[][NUM]);
void matrixCpy(char dst[][NUM], char src[][NUM]);
void matrixPrint(char matrix[][NUM]);
void bgOpenLog(void);
void bgWriteLog(const char* s);
void bgCloseLog(void);
int main(void)
{
Matrix src;
bgOpenLog();
initQueue(3628800);
initStack(1000);
src.v = 2;
src.w = 2;
matrixCpy(src.matrix, srcMatrix);
src.pre = cnt;
bfs(src);
bgCloseLog();
getchar();
return 0;
}
void bfs(Matrix matrix)
{
Matrix s, t;
int v, w;
int direction, tmp;
putQueue(matrix);
while (!isQueueEmpty())
{
t = getQueue();
if (!matrixCmp(t.matrix, dstMatrix))
{
cnt++;
for (direction = 0; direction < 4; direction++)
{
s = t;
v = s.v + dx[direction]; w = s.w + dy[direction];
if (srcMatrix[v][w] != '*')
{
tmp = s.matrix[s.v][s.w];
s.matrix[s.v][s.w] = s.matrix[v][w];
s.matrix[v][w] = tmp;
s.v = v;
s.w = w;
s.pre = cnt;
for (tmp = 0; tmp < queue->tail; tmp++)
{
if (matrixCmp(queue->data[tmp].matrix, s.matrix))
{
break;
}
}
if (tmp == queue->tail)
{
putQueue(s);
}
}
}
}
else
{
pushStack(t);
for (tmp = t.pre; !matrixCmp(queue->data[tmp].matrix, srcMatrix); tmp = queue->data[tmp].pre)
{
pushStack(queue->data[tmp]);
}
matrixCpy(t.matrix, srcMatrix);
pushStack(t);
while (!isStackEmpty())
{
t = popStack();
matrixPrint(t.matrix);
}
printf("Hi, BlueGuy");
return;
}
}
}
void initQueue(int length)
{
queue = malloc(sizeof(BGQueue));
queue->data = malloc(sizeof(Matrix) * length);
queue->maxLength = length;
queue->head = 0;
queue->tail = 0;
}
void putQueue(Matrix matrix)
{
queue->data[queue->tail++] = matrix;
queue->tail = queue->tail % queue->maxLength;
}
Matrix getQueue(void)
{
Matrix ret;
ret = queue->data[queue->head++];
queue->head = queue->head % queue->maxLength;
return ret;
}
int isQueueEmpty(void)
{
return queue->head == queue->tail;
}
int isQueueFull(void)
{
return ((queue->tail+1) % queue->maxLength) == queue->head;
}
void initStack(int length)
{
stack = malloc(sizeof(BGStack));
stack->data = malloc(sizeof(Matrix) * length);
stack->top = 0;
}
void pushStack(Matrix matrix)
{
stack->data[stack->top++] = matrix;
}
Matrix popStack(void)
{
Matrix ret;
ret = stack->data[--stack->top];
return ret;
}
int isStackEmpty(void)
{
return (stack->top == 0);
}
int matrixCmp(char src[][NUM], char dst[][NUM])
{
int i, j, s, t, ret;
char srcString[30] = {0};
char dstString[30] = {0};
s = 0;
t = 0;
for (i = 0; i < NUM; i++)
{
for (j = 0; j < NUM; j++)
{
srcString[s++] = src[i][j];
dstString[t++] = dst[i][j];
}
}
ret = !strcmp(srcString, dstString);
return ret;
}
void matrixCpy(char dst[][NUM], char src[][NUM])
{
int i, j;
for (i = 0; i < NUM; i++)
{
for (j = 0; j < NUM; j++)
{
dst[i][j] = src[i][j];
}
}
}
void matrixPrint(char matrix[][NUM])
{
char logTxt[60] = {0};
int i, j, k;
k = 0;
for (i = 0; i < NUM; i++)
{
for (j = 0; j < NUM; j++)
{
logTxt[k++] = matrix[i][j];
}
logTxt[k++] = '\r';
logTxt[k++] = '\n';
}
logTxt[k++] = '\r';
logTxt[k++] = '\n';
bgWriteLog(logTxt);
}
void bgOpenLog(void)
{
if(log == NULL)
{
log = fopen("log.txt", "wb");
}
}
void bgWriteLog(const char* s)
{
fwrite(s, sizeof(char), strlen(s), log);
}
void bgCloseLog(void)
{
fclose(log);
log = NULL;
}
[
本帖最后由 BlueGuy 于 2015-6-4 17:57 编辑 ]