问题描述
![问题描述](https://img-blog.csdnimg.cn/e617f03ada62439cb6cac9af26a1408d.png#pic_center)初始状态的步数就算1,哈哈
输入:第一个33的矩阵是原始状态,第二个33的矩阵是目标状态。
输出:移动所用最少的步数
Input
2 8 3
1 6 4
7 0 5
1 2 3
8 0 4
7 6 5
Output
6
代码部分
因为最近在准备复试,所以具体的注释啥的就等以后有时间了再详细记录,主要就是用BFS的思路解决,菜鸡代码,大佬勿喷。 唯一提醒的是,数组一定要开的够大,不然无法通过//
// Created by hwb on 2022/3/21.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int a[3][3];
int flag;
int id;
int step;
int zero_x;
int zero_y;
} Node;
typedef struct Queue {
Node nodes[362890];
int rare;
int front;
} Queue;
typedef struct VisitNode {
int id;
// int flag;
} VisNode;
typedef struct Vis {
VisNode visNode[362890];
int count;
} Vis;
int changeToId(Node *node) {
int id = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
id = id * 10 + node->a[i][j];
}
}
node->id = id;
return id;
}
Node nodes[362890];
Vis vis;//用于标记是否被访问过
Queue queue;
struct His{
int ids[362890];
int count;
} history;
void push_back(Node *node) {
queue.rare++;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
queue.nodes[queue.rare].a[i][j] = node->a[i][j];
}
}
queue.nodes[queue.rare].id = node->id;
queue.nodes[queue.rare].flag = node->flag;
queue.nodes[queue.rare].step = node->step;
queue.nodes[queue.rare].zero_x = node->zero_x;
queue.nodes[queue.rare].zero_y = node->zero_y;
}
Node *pop() {
Node *ret = (Node *) malloc(sizeof(Node));
ret->id = queue.nodes[queue.front].id;
ret->flag = queue.nodes[queue.front].flag;
ret->step = queue.nodes[queue.front].step;
ret->zero_x = queue.nodes[queue.front].zero_x;
ret->zero_y = queue.nodes[queue.front].zero_y;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
ret->a[i][j] = queue.nodes[queue.front].a[i][j];
}
}
queue.front++;
return ret;
}
void input(int a[][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &a[i][j]);
}
}
}
void setZero(Node *node) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (node->a[i][j] == 0) {
node->zero_x = i;
node->zero_y = j;
break;
}
}
}
}
int hasVis(int id) {
int ret = 0;
for (int i = 0; i < vis.count; ++i) {
if (vis.visNode[i].id == id) {
ret = 1;
return ret;
}
}
return ret;
}
void visit(int id) {
vis.visNode[++vis.count].id = id;
}
void swap(Node *node, int x1, int y1, int x2, int y2) {
int temp = node->a[x1][y1];
node->a[x1][y1] = node->a[x2][y2];
node->a[x2][y2] = temp;
}
int test(int x1,int y1,int x2, int y2){
if(x1+x2 >=0 && x1+x2 <3&& y1+y2 >=0&& y1+y2<3)
return 1;
else
return 0;
}
void print(Node * node){
printf("-------%d----------\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ",node->a[i][j]);
}
printf("\n");
}
}
int main() {
int flag= 0;
history.count = 0;
vis.count = 0;
Node origin, final;
input(origin.a);
input(final.a);
queue.front = 0;
queue.rare = -1;
setZero(&origin);
setZero(&final);
changeToId(&origin);
changeToId(&final);
origin.step = 1;
origin.flag = 0;
push_back(&origin);
visit(origin.id);
int X[] = {1, 0, -1, 0};
int Y[] = {0, 1, 0, -1};
while (queue.rare >= queue.front) {
Node *temp = pop();
if(final.id == temp->id){
printf("%d\n",temp->step);
flag = 1;
break;
}
for (int i = 0; i < 4; ++i) {
if(test(temp->zero_x,temp->zero_y,X[i],Y[i])){
swap(temp,temp->zero_x,temp->zero_y,
temp->zero_x+X[i],temp->zero_y+Y[i]);
changeToId(temp);
setZero(temp);
temp->step = temp->step+1;
if (!hasVis(temp->id)){
push_back(temp);
visit(temp->id);
}
if(flag){
printf(temp);
}
swap(temp,temp->zero_x,temp->zero_y,
temp->zero_x-X[i],temp->zero_y-Y[i]);
temp->step = temp ->step -1;
changeToId(temp);
setZero(temp);
}
}
flag = 0;
}
}