#include<stdio.h> #include<mem.h> #define OK 1 #define ERROR 0 #define MAX_SIZE 100 typedef int Status; typedef struct{ int nums[3]; int id; int operation; int parentId; int level; }ElemType; int cmpArray(int a[], int b[], int n) { int i; for(i=0;i<n;i++) if(a[i] != b[i]) return false; return true; } void BFS(int start[3], int end[3]) { ElemType openTable[MAX_SIZE]; int openFront = 0; int openRear = 0; ElemType CloseTable[MAX_SIZE]; int closeLen=0; bool isSuccess = false; ElemType tmpElem; int count; for(int i=0;i<3;i++) tmpElem.nums[i] = start[i]; // memcpy(tmpElem.nums, start, sizeof(start)); printf("%d %d %d\n", tmpElem.nums[0],tmpElem.nums[1],tmpElem.nums[2]); tmpElem.level = 1; tmpElem.parentId = -1; tmpElem.operation = -1; openTable[openRear] = tmpElem; openRear = (openRear + 1) % MAX_SIZE; while(openRear != openFront) { tmpElem = openTable[openFront]; openFront = (openFront + 1)%MAX_SIZE; printf("[%d %d %d], %c, %d\n", tmpElem.nums[0], tmpElem.nums[1] , tmpElem.nums[2], tmpElem.operation, tmpElem.level ); if(cmpArray(tmpElem.nums, end, 3) && tmpElem.level == 4) { isSuccess = true; break; } else if(tmpElem.level >= 4) { break; } ElemType childElem; for(int i=0;i<3;i++) { if(tmpElem.operation != 'a'+i) { count++; for(int i=0;i<3;i++) childElem.nums[i] = tmpElem.nums[i]; childElem.operation = 'a' + i; // memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i] = (childElem.nums[i] + 1) % 2; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1)%MAX_SIZE; } } } if(isSuccess) { printf("success!"); } else { printf("failed"); } } int main() { int a[3]={0,1,0}; int b[3]={1,1,1}; int c[3] = {0,0,0}; BFS(a,b); return 0; } 使用以上程序框架编写C语言代码,解决八数码问题
时间: 2024-04-12 11:34:27 浏览: 98
您可以使用以下代码来解决八数码问题:
```c
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define MAX_SIZE 100
typedef struct{
int nums[3][3];
int id;
int operation;
int parentId;
int level;
}ElemType;
void printMatrix(int matrix[3][3]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
bool cmpMatrix(int a[3][3], int b[3][3]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j] != b[i][j])
return false;
}
}
return true;
}
void BFS(int start[3][3], int end[3][3]){
ElemType openTable[MAX_SIZE];
int openFront = 0;
int openRear = 0;
ElemType CloseTable[MAX_SIZE];
int closeLen=0;
bool isSuccess = false;
ElemType tmpElem;
int count = 0;
memcpy(tmpElem.nums, start, sizeof(start));
printMatrix(tmpElem.nums);
tmpElem.level = 1;
tmpElem.parentId = -1;
tmpElem.operation = -1;
openTable[openRear] = tmpElem;
openRear = (openRear + 1) % MAX_SIZE;
while(openRear != openFront){
tmpElem = openTable[openFront];
openFront = (openFront + 1) % MAX_SIZE;
printMatrix(tmpElem.nums);
printf("Operation: %c, Level: %d\n", tmpElem.operation, tmpElem.level);
if(cmpMatrix(tmpElem.nums, end) && tmpElem.level == 4){
isSuccess = true;
break;
}
else if(tmpElem.level >= 4){
break;
}
ElemType childElem;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(tmpElem.nums[i][j] == 0){
if(i > 0 && tmpElem.operation != 'd'){ // move up
count++;
memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums));
childElem.nums[i][j] = childElem.nums[i-1][j];
childElem.nums[i-1][j] = 0;
childElem.operation = 'u';
childElem.parentId = tmpElem.id;
childElem.id = count;
childElem.level = tmpElem.level + 1;
openTable[openRear] = childElem;
openRear = (openRear + 1) % MAX_SIZE;
}
if(i < 2 && tmpElem.operation != 'u'){ // move down
count++;
memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums));
childElem.nums[i][j] = childElem.nums[i+1][j];
childElem.nums[i+1][j] = 0;
childElem.operation = 'd';
childElem.parentId = tmpElem.id;
childElem.id = count;
childElem.level = tmpElem.level + 1;
openTable[openRear] = childElem;
openRear = (openRear + 1) % MAX_SIZE;
}
if(j > 0 && tmpElem.operation != 'r'){ // move left
count++;
memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums));
childElem.nums[i][j] = childElem.nums[i][j-1];
childElem.nums[i][j-1] = 0;
childElem.operation = 'l';
childElem.parentId = tmpElem.id;
childElem.id = count;
childElem.level = tmpElem.level + 1;
openTable[openRear] = childElem;
openRear = (openRear + 1) % MAX_SIZE;
}
if(j < 2 && tmpElem.operation != 'l'){ // move right
count++;
memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums));
childElem.nums[i][j] = childElem.nums[i][j+1];
childElem.nums[i][j+1] = 0;
childElem.operation = 'r';
childElem.parentId = tmpElem.id;
childElem.id = count;
childElem.level = tmpElem.level + 1;
openTable[openRear] = childElem;
openRear = (openRear + 1) % MAX_SIZE;
}
}
}
}
}
if(isSuccess){
printf("Success!\n");
}
else{
printf("Failed\n");
}
}
int main(){
int start[3][3] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
int end[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
BFS(start, end);
return 0;
}
```
在上述代码中,`start`和`end`分别表示初始状态和目标状态的矩阵。`BFS()`函数使用广度优先搜索算法来解决八数码问题。在该函数中,我们使用了一个队列来存储待扩展的节点,以及一个数组来存储已扩展的节点。通过遍历每个节点的相邻节点,并将其加入队列中,直到找到目标状态或达到最大搜索深度。如果找到目标状态,则输出"Success!",否则输出"Failed"。
阅读全文