日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

八数码c语言编程深度搜索,广度优先搜索解 八数码, 求意见, 求bug/

發布時間:2024/9/27 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 八数码c语言编程深度搜索,广度优先搜索解 八数码, 求意见, 求bug/ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

已結貼√

問題點數:100?回復次數:31

廣度優先搜索解 , 求意見, 求bug/

我得去買票了,....

#include

#include

#include

#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;

char srcMatrix[NUM][NUM] =

{

{'*', '*', '*', '*', '*'},

{'*', '1', '2', '3', '*'},

{'*', '4', '5', '6', '*'},

{'*', '7', '8', '0', '*'},

{'*', '*', '*', '*', '*'}

};

char dstMatrix[NUM][NUM] =

{

{'*', '*', '*', '*', '*'},

{'*', '1', '2', '0', '*'},

{'*', '4', '5', '3', '*'},

{'*', '7', '8', '6', '*'},

{'*', '*', '*', '*', '*'}

};

int dx[4] = {0, 0, -1, 1};

int dy[4] = {-1, 1, 0, 0};

int cnt = -1;

Queue queue;

FILE* log;

void bfs(Matrix matrix);

void initQueue(int length);

void putQueue(Matrix matrix);

Matrix getQueue(void);

int isQueueEmpty(void);

int isQueueFull(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(5200);

src.v = 3;

src.w = 3;

matrixCpy(src.matrix, srcMatrix);

src.pre = cnt;

bfs(src);

bgCloseLog();

getchar();

return 0;

}

void bfs(Matrix matrix)

{

Matrix 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++)

{

v = t.v + dx[direction]; w = t.w + dy[direction];

if (srcMatrix[v][w] != '*')

{

tmp = t.matrix[t.v][t.w];

t.matrix[t.v][t.w] = t.matrix[v][w];

t.matrix[v][w] = tmp;

t.v = v;

t.w = w;

t.pre = cnt;

putQueue(t);

}

}

}

else

{

matrixPrint(t.matrix);

for (tmp = t.pre; !matrixCmp(queue->data[tmp].matrix, srcMatrix); tmp = queue->data[tmp].pre)

{

matrixPrint(queue->data[tmp].matrix);

}

matrixPrint(srcMatrix);

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;

}

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;

}

總結

以上是生活随笔為你收集整理的八数码c语言编程深度搜索,广度优先搜索解 八数码, 求意见, 求bug/的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。