宽度优先搜索解决八数码c语言

时间: 2023-10-26 10:16:21 浏览: 30
以下是使用宽度优先搜索算法解决八数码问题的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 3 typedef struct state { int board[SIZE][SIZE]; int blankRow, blankCol; struct state *prev; } State; typedef struct node { State *state; struct node *next; } Node; typedef struct queue { Node *head; Node *tail; } Queue; State *newState(int board[SIZE][SIZE], int blankRow, int blankCol, State *prev) { State *state = (State *) malloc(sizeof(State)); memcpy(state->board, board, sizeof(int) * SIZE * SIZE); state->blankRow = blankRow; state->blankCol = blankCol; state->prev = prev; return state; } void printBoard(int board[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%d ", board[i][j]); } printf("\n"); } } void printSolution(State *state) { int steps = 0; while (state->prev) { steps++; state = state->prev; } printf("Solution in %d steps:\n", steps); while (state) { printBoard(state->board); printf("\n"); state = state->prev; } } void enqueue(Queue *queue, State *state) { Node *node = (Node *) malloc(sizeof(Node)); node->state = state; node->next = NULL; if (queue->tail) { queue->tail->next = node; } else { queue->head = node; } queue->tail = node; } State *dequeue(Queue *queue) { Node *head = queue->head; State *state = head->state; queue->head = head->next; free(head); if (!queue->head) { queue->tail = NULL; } return state; } int isGoalState(int board[SIZE][SIZE]) { int n = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] != n++) { return 0; } } } return 1; } void freeState(State *state) { free(state); } void freeQueue(Queue *queue) { while (queue->head) { State *state = dequeue(queue); freeState(state); } } void solve(int startBoard[SIZE][SIZE]) { Queue *queue = (Queue *) malloc(sizeof(Queue)); queue->head = NULL; queue->tail = NULL; State *startState = newState(startBoard, 0, 0, NULL); enqueue(queue, startState); while (queue->head) { State *state = dequeue(queue); if (isGoalState(state->board)) { printSolution(state); freeQueue(queue); freeState(state); return; } int row = state->blankRow; int col = state->blankCol; if (row > 0) { int newBoard[SIZE][SIZE]; memcpy(newBoard, state->board, sizeof(int) * SIZE * SIZE); newBoard[row][col] = newBoard[row - 1][col]; newBoard[row - 1][col] = 0; State *newState = newState(newBoard, row - 1, col, state); enqueue(queue, newState); } if (row < SIZE - 1) { int newBoard[SIZE][SIZE]; memcpy(newBoard, state->board, sizeof(int) * SIZE * SIZE); newBoard[row][col] = newBoard[row + 1][col]; newBoard[row + 1][col] = 0; State *newState = newState(newBoard, row + 1, col, state); enqueue(queue, newState); } if (col > 0) { int newBoard[SIZE][SIZE]; memcpy(newBoard, state->board, sizeof(int) * SIZE * SIZE); newBoard[row][col] = newBoard[row][col - 1]; newBoard[row][col - 1] = 0; State *newState = newState(newBoard, row, col - 1, state); enqueue(queue, newState); } if (col < SIZE - 1) { int newBoard[SIZE][SIZE]; memcpy(newBoard, state->board, sizeof(int) * SIZE * SIZE); newBoard[row][col] = newBoard[row][col + 1]; newBoard[row][col + 1] = 0; State *newState = newState(newBoard, row, col + 1, state); enqueue(queue, newState); } freeState(state); } freeQueue(queue); printf("No solution found\n"); } int main() { int board[SIZE][SIZE] = {{1, 2, 3}, {4, 0, 5}, {6, 7, 8}}; solve(board); return 0; } ``` 该代码使用了队列来实现宽度优先搜索。每个状态都包含一个指向前一个状态的指针,最终可以从最终状态沿着指针链回溯到初始状态,从而输出解决方案。

相关推荐

最新推荐

recommend-type

八数码问题宽度优先搜索

给大家编好了一个八数码宽度搜索算法!使用C语言编写的!绝对运行!不信试试看!如果好大家给个评价!
recommend-type

C语言使用广度优先搜索算法解决迷宫问题(队列)

主要介绍了C语言使用广度优先搜索算法解决迷宫问题,结合迷宫问题分析了C语言队列广度优先搜索算法的相关使用技巧,需要的朋友可以参考下
recommend-type

C语言基于回溯算法解决八皇后问题的方法

主要介绍了C语言基于回溯算法解决八皇后问题的方法,简单描述了八皇后问题,并结合实例形式分析了C语言使用回溯算法解决八皇后问题的相关操作技巧,需要的朋友可以参考下
recommend-type

C语言中计算二叉树的宽度的两种方式

主要介绍了C语言中计算二叉树的宽度的两种方式的相关资料,需要的朋友可以参考下
recommend-type

解决C语言数组元素循环右移的问题

今天小编就为大家分享一篇解决C语言数组元素循环右移的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

从键盘输入一段英文字符串,其中包含多个字母‘h',请编写程序利用正则表达式,将英文字符串中的’h'全部改为‘H’

好的,我可以帮您解决这个问题。 您可以使用 Python 编写一个程序来实现这个功能。具体步骤如下: 1. 首先导入 re 模块,这个模块是 Python 中用于处理正则表达式的模块。 2. 然后定义一个包含英文字符串的变量,例如: text = "This is a string containing multiple h's." 3. 接下来,使用 re.sub() 方法来进行替换操作。re.sub() 方法的第一个参数是正则表达式,第二个参数是替换的字符或字符串,第三个参数是被替换的字符串。在这个例子里,我们需要将所有的小写字母 h 替换成大写字母 H,所以正则表达式可以写成
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。