八数码问题求解
(一)实验软件
TC2.0或VC6.0编程语言或其它编程语言
(二)实验目的
1. 熟悉人工智能系统中的问题求解过程;
2. 熟悉状态空间的盲目搜索和启发式搜索算法的应用;
3. 熟悉对八数码问题的建模,求解及编程语言的应用。
(三)实验内容
八数码问题:在3×3的方格棋盘上,摆放着1到8这八个数码,有一个方格是空的,要求对空格执行空格左移,空格右移,空格上移,空格下移这四个操作使得棋盘从初始状态到目标状态。输入初始状态和目标状态,输出从初始状态到目标状态的路径。
(四)实验代码
#include"stdafx.h"
#include
#include
#include
usingnamespace std;
constint ROW = 3;
constint COL = 3;
constint MAXDISTANCE = 10000;
constint MAXNUM = 10000;
typedefstruct _Node{
int digit[ROW][COL];
int dist; // distance between one state and the destination int dep; // the depth of node
// So the comment function = dist + dep.
int index; // point to the location of parent
} Node ;
Node src, dest;
vector node_v; // store the nodes
bool isEmptyOfOPEN() {
for (int i = 0; i
if (node_v[i].dist != MAXNUM)
returnfalse ;
}
returntrue ;
}
bool isEqual(int index , int digit [][COL]) {
for (int i = 0; i
for (int j = 0; j
if (node_v[index ].digit[i][j] != digit [i][j])
returnfalse ;
}
returntrue ;
}
ostream & operator
for (int i = 0; i
for (int j = 0; j
os
os
}
return os ;
}
void PrintSteps(int index , vector &rstep_v) {
rstep_v.push_back(node_v[index ]);
index = node_v[index ].index;
while (index != 0) {
rstep_v.push_back(node_v[index ]);
index = node_v[index ].index;
}
for (int i = rstep_v.size() - 1; i >= 0; i--)
cout
}
void Swap(int &a , int &b ) {
int t;
t = a ;
a = b ;
b = t;
}
void Assign(Node &node , int index ) {
for (int i = 0; i
for (int j = 0; j
node .digit[i][j] = node_v[index ].digit[i][j];
}
int GetMinNode() {
int dist = MAXNUM;
int loc; // the location of minimize node
for (int i = 0; i
if (node_v[i].dist == MAXNUM)
continue ;
elseif ((node_v[i].dist + node_v[i].dep)
loc = i;
dist = node_v[i].dist + node_v[i].dep;
}
}
return loc;
}
bool isExpandable(Node &node ) {
for (int i = 0; i
if (isEqual(i, node .digit))
returnfalse ;
}
returntrue ;
}//扩展
int Distance(Node &node , int digit [][COL]) {
int distance = 0;
bool flag = false ;
for (int i = 0; i
for (int j = 0; j
for (int k = 0; k
for (int l = 0; l
if (node .digit[i][j] == digit [k][l]) {
distance += abs(i - k) + abs(j - l);//abs()求得是正数的绝对值。
flag = true ;
break ;
}
else
flag = false ;
}
if (flag)
break ;
}
return distance;
}
int MinDistance(int a , int b ) {
return (a
}
void ProcessNode(int index ) {
int x, y;
bool flag;
for (int i = 0; i
for (int j = 0; j
if (node_v[index ].digit[i][j] == 0) {
x = i; y = j;
flag = true ;
break ;
}
else flag = false ;
}
if (flag)
break ;
}
Node node_up;
Assign(node_up, index );
int dist_up = MAXDISTANCE;
if (x > 0) {
Swap(node_up.digit[x][y], node_up.digit[x - 1][y]); if (isExpandable(node_up)) {
dist_up = Distance(node_up, dest.digit);
node_up.index = index ;
node_up.dist = dist_up;
node_up.dep = node_v[index ].dep + 1;
node_v.push_back(node_up);
}
}
Node node_down;
Assign(node_down, index );
int dist_down = MAXDISTANCE;
if (x
Swap(node_down.digit[x][y], node_down.digit[x + 1][y]);
if (isExpandable(node_down)) {
dist_down = Distance(node_down, dest.digit); node_down.index = index ;
node_down.dist = dist_down;
node_down.dep = node_v[index ].dep + 1;
node_v.push_back(node_down);
} }
Node node_left;
Assign(node_left, index );
int dist_left = MAXDISTANCE;
if (y > 0) {
Swap(node_left.digit[x][y], node_left.digit[x][y - 1]); if (isExpandable(node_left)) {
dist_left = Distance(node_left, dest.digit); node_left.index = index ;
node_left.dist = dist_left;
node_left.dep = node_v[index ].dep + 1;
node_v.push_back(node_left);
}
}
Node node_right;
Assign(node_right, index );
int dist_right = MAXDISTANCE;
if (y
Swap(node_right.digit[x][y], node_right.digit[x][y + 1]); if (isExpandable(node_right)) {
dist_right = Distance(node_right, dest.digit); node_right.index = index ;
node_right.dist = dist_right;
node_right.dep = node_v[index ].dep + 1;
node_v.push_back(node_right);
}
}
node_v[index ].dist = MAXNUM;
}
int main() {
int number;
cout
for (int i = 0; i
for (int j = 0; j
cin >> number;
src.digit[i][j] = number;
}
src.index = 0;
src.dep = 1;
cout
for (int m = 0; m
for (int n = 0; n
cin >> number;
dest.digit[m][n] = number;
}
node_v.push_back(src);
cout
clock_t start = clock();
while (1) {
if (isEmptyOfOPEN()) {
cout
}
else {
int loc; // the location of the minimize node loc = GetMinNode();
if (isEqual(loc, dest.digit)) {
vector rstep_v;
cout
cout
PrintSteps(loc, rstep_v);
cout
cout
break ;
}
else
ProcessNode(loc);
}
}
system("Pause" );
}
(五)实验结果截图
八数码问题求解
(一)实验软件
TC2.0或VC6.0编程语言或其它编程语言
(二)实验目的
1. 熟悉人工智能系统中的问题求解过程;
2. 熟悉状态空间的盲目搜索和启发式搜索算法的应用;
3. 熟悉对八数码问题的建模,求解及编程语言的应用。
(三)实验内容
八数码问题:在3×3的方格棋盘上,摆放着1到8这八个数码,有一个方格是空的,要求对空格执行空格左移,空格右移,空格上移,空格下移这四个操作使得棋盘从初始状态到目标状态。输入初始状态和目标状态,输出从初始状态到目标状态的路径。
(四)实验代码
#include"stdafx.h"
#include
#include
#include
usingnamespace std;
constint ROW = 3;
constint COL = 3;
constint MAXDISTANCE = 10000;
constint MAXNUM = 10000;
typedefstruct _Node{
int digit[ROW][COL];
int dist; // distance between one state and the destination int dep; // the depth of node
// So the comment function = dist + dep.
int index; // point to the location of parent
} Node ;
Node src, dest;
vector node_v; // store the nodes
bool isEmptyOfOPEN() {
for (int i = 0; i
if (node_v[i].dist != MAXNUM)
returnfalse ;
}
returntrue ;
}
bool isEqual(int index , int digit [][COL]) {
for (int i = 0; i
for (int j = 0; j
if (node_v[index ].digit[i][j] != digit [i][j])
returnfalse ;
}
returntrue ;
}
ostream & operator
for (int i = 0; i
for (int j = 0; j
os
os
}
return os ;
}
void PrintSteps(int index , vector &rstep_v) {
rstep_v.push_back(node_v[index ]);
index = node_v[index ].index;
while (index != 0) {
rstep_v.push_back(node_v[index ]);
index = node_v[index ].index;
}
for (int i = rstep_v.size() - 1; i >= 0; i--)
cout
}
void Swap(int &a , int &b ) {
int t;
t = a ;
a = b ;
b = t;
}
void Assign(Node &node , int index ) {
for (int i = 0; i
for (int j = 0; j
node .digit[i][j] = node_v[index ].digit[i][j];
}
int GetMinNode() {
int dist = MAXNUM;
int loc; // the location of minimize node
for (int i = 0; i
if (node_v[i].dist == MAXNUM)
continue ;
elseif ((node_v[i].dist + node_v[i].dep)
loc = i;
dist = node_v[i].dist + node_v[i].dep;
}
}
return loc;
}
bool isExpandable(Node &node ) {
for (int i = 0; i
if (isEqual(i, node .digit))
returnfalse ;
}
returntrue ;
}//扩展
int Distance(Node &node , int digit [][COL]) {
int distance = 0;
bool flag = false ;
for (int i = 0; i
for (int j = 0; j
for (int k = 0; k
for (int l = 0; l
if (node .digit[i][j] == digit [k][l]) {
distance += abs(i - k) + abs(j - l);//abs()求得是正数的绝对值。
flag = true ;
break ;
}
else
flag = false ;
}
if (flag)
break ;
}
return distance;
}
int MinDistance(int a , int b ) {
return (a
}
void ProcessNode(int index ) {
int x, y;
bool flag;
for (int i = 0; i
for (int j = 0; j
if (node_v[index ].digit[i][j] == 0) {
x = i; y = j;
flag = true ;
break ;
}
else flag = false ;
}
if (flag)
break ;
}
Node node_up;
Assign(node_up, index );
int dist_up = MAXDISTANCE;
if (x > 0) {
Swap(node_up.digit[x][y], node_up.digit[x - 1][y]); if (isExpandable(node_up)) {
dist_up = Distance(node_up, dest.digit);
node_up.index = index ;
node_up.dist = dist_up;
node_up.dep = node_v[index ].dep + 1;
node_v.push_back(node_up);
}
}
Node node_down;
Assign(node_down, index );
int dist_down = MAXDISTANCE;
if (x
Swap(node_down.digit[x][y], node_down.digit[x + 1][y]);
if (isExpandable(node_down)) {
dist_down = Distance(node_down, dest.digit); node_down.index = index ;
node_down.dist = dist_down;
node_down.dep = node_v[index ].dep + 1;
node_v.push_back(node_down);
} }
Node node_left;
Assign(node_left, index );
int dist_left = MAXDISTANCE;
if (y > 0) {
Swap(node_left.digit[x][y], node_left.digit[x][y - 1]); if (isExpandable(node_left)) {
dist_left = Distance(node_left, dest.digit); node_left.index = index ;
node_left.dist = dist_left;
node_left.dep = node_v[index ].dep + 1;
node_v.push_back(node_left);
}
}
Node node_right;
Assign(node_right, index );
int dist_right = MAXDISTANCE;
if (y
Swap(node_right.digit[x][y], node_right.digit[x][y + 1]); if (isExpandable(node_right)) {
dist_right = Distance(node_right, dest.digit); node_right.index = index ;
node_right.dist = dist_right;
node_right.dep = node_v[index ].dep + 1;
node_v.push_back(node_right);
}
}
node_v[index ].dist = MAXNUM;
}
int main() {
int number;
cout
for (int i = 0; i
for (int j = 0; j
cin >> number;
src.digit[i][j] = number;
}
src.index = 0;
src.dep = 1;
cout
for (int m = 0; m
for (int n = 0; n
cin >> number;
dest.digit[m][n] = number;
}
node_v.push_back(src);
cout
clock_t start = clock();
while (1) {
if (isEmptyOfOPEN()) {
cout
}
else {
int loc; // the location of the minimize node loc = GetMinNode();
if (isEqual(loc, dest.digit)) {
vector rstep_v;
cout
cout
PrintSteps(loc, rstep_v);
cout
cout
break ;
}
else
ProcessNode(loc);
}
}
system("Pause" );
}
(五)实验结果截图