#include<iostream>
#include<time h="">
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
int mubiao[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
int current[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
int dir[4][2] = { { 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 } };
struct node {
int row;
int col;
}];
int manhattan() {
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int row, col;
if (current[i][j] == 0) {
continue;
}
else {
row = (current[i][j] - 1) / 3;
col = (current[i][j] - 1) % 3;
}
sum += abs(i - row) + abs(j - col);
}
}
return sum;
}
bool hill_climbing() {
int row;
int col;
int flag = 0;
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (current[j][k] == 0) {
row = j;
col = k;
flag = 1;
break;
}
}
if (flag == 1) break;
}
int h;
int rr, cc, ff, step = 0;
do {
h = 999999;
for (int i = 0; i < 4; i++) {
int r = row + dir[i][0];
int c = col + dir[i][1];
if (r >= 0 && r < 3 && c >= 0 && c < 3) {
swap(current[r][c],
人工智能-8-puzzle爬山法(最陡上升,首项选择),随即重新启动法
最新推荐文章于 2024-04-20 18:30:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)