八数码问题的A*算法求解

A*算法是启发式搜素算法中较为出名和高效的算法之一,其关键是对于启发式函数的实际,启发式函数h(x)需要尽可能的接近实际的h(x)^\*。下面是人工智能八数码问题使用A*算法求解的源码放在博客上记录一下。程序使用放错位置的棋子的个数作为启发式函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <vector>
#include <queue>

using namespace std;


class Picture {
private:

int **pictureArray = NULL;
int hValue;
int gValue;
int fValue;
public:
Picture *parente=NULL;
int zeroRow, zeroColumn;
static int rowSize, columnSize;

static void setSize(int row, int column) {
rowSize = row;
columnSize = column;
}

Picture() {
if (rowSize == 0 || columnSize == 0)
throw "no init";
pictureArray = new int *[rowSize];
for (int i = 0; i < rowSize; ++i) {
pictureArray[i] = new int[columnSize];
}
hValue = 0;
gValue = 0;
fValue = 0;
setArray();
}

Picture(const Picture &sourse) {
zeroRow = sourse.getZeroRow();
zeroColumn = sourse.getZeroColumn();
parente = sourse.parente;
pictureArray = new int *[rowSize];
for (int i = 0; i < rowSize; ++i) {
pictureArray[i] = new int[columnSize];
}
for (int i = 0; i < rowSize; ++i)
for (int j = 0; j < columnSize; ++j) {
pictureArray[i][j] = sourse.getPicturePoint()[i][j];
}
}

~Picture() {
delete[] pictureArray;
}

bool operator==(const Picture &source) const {
for (int i = 0; i < rowSize; ++i)
for (int j = 0; j < columnSize; ++j) {
if (pictureArray[i][j] != source.getPicturePoint()[i][j])
return false;
}
return true;
}

void setArray() {
for (int i = 0; i < rowSize; ++i) {
for (int j = 0; j < columnSize; ++j) {
cin >> pictureArray[i][j];
if (pictureArray[i][j] == 0) {
zeroRow = i;
zeroColumn = j;
}
}
}
}


/*
* 设置启发式函数的值
*/
void setHValue(const Picture &endPicture) {
int result = 0;
int **tempPicture = endPicture.getPicturePoint();
for (int i = 0; i < rowSize; ++i)
for (int j = 0; j < columnSize; ++j) {
if (pictureArray[i][j] != tempPicture[i][j])
++result;
}
hValue = result;
}

void updateFvalue() {
fValue = hValue + gValue;
}

void setGvalue(int value) {
gValue = value;
}

int **getPicturePoint() const {
return pictureArray;
}

int getFValue() const {
return fValue;
}

int getGvalue() const {
return gValue;
}

int getZeroRow() const {
return zeroRow;
}

int getZeroColumn() const {
return zeroColumn;
}

void showPicture() const {
for (int i = 0; i < rowSize; ++i) {
for (int j = 0; j < columnSize - 1; ++j) {
cout << pictureArray[i][j] << ' ';
}
cout << pictureArray[i][columnSize - 1] << endl;
}
cout<<endl;
}
};


int inVector(vector<Picture *> const &theVector, Picture const &element) {
for (int i = 0; i < theVector.size(); ++i)
if (element == *theVector[i])
return i;
return -1;
}

void deleteElement(vector<Picture *> &theVector, const Picture *element) {
for (int i = 0; i < theVector.size(); ++i) {
if (element == theVector[i]) {
theVector.erase(theVector.begin() + i);
return;
}
}
}

struct cmp {
bool operator()(const Picture *a, const Picture *b) {
return a->getFValue() > b->getFValue();
}
};

bool Astart(Picture *&beginPicture, Picture *&endPicture) {
priority_queue<Picture *, vector<Picture *>, cmp> openQueue;
vector<Picture *> openTable, closeTable;
beginPicture->setHValue(*endPicture);
beginPicture->updateFvalue();
openQueue.push(beginPicture);
openTable.push_back(beginPicture);
int move[4][2] = { {-1, 0},
{1, 0},
{0, -1},
{0, 1}};
while (!openQueue.empty()) {
Picture *bestPicture = openQueue.top();
if (*bestPicture == *endPicture) {
delete endPicture;
endPicture = bestPicture;
return true;
}
closeTable.push_back(bestPicture);
openQueue.pop();
deleteElement(openTable, bestPicture);
//向上下左右四个方向进行拓展
for (int i = 0; i < 4; ++i) {
int row = bestPicture->zeroRow + move[i][0];
int column = bestPicture->zeroColumn + move[i][1];
if (row >= 0 && row < Picture::rowSize && column >= 0 && column < Picture::columnSize) {
Picture *successor = new Picture(*bestPicture);
int **theArray = successor->getPicturePoint();
theArray[successor->zeroRow][successor->zeroColumn] = theArray[row][column];
theArray[row][column] = 0;
successor->zeroRow = row;
successor->zeroColumn = column;
successor->parente = bestPicture;
successor->setGvalue(bestPicture->getGvalue() + 1);
int flag = inVector(openTable, *successor);
if (flag >= 0) {
if (successor->getGvalue() < openTable[flag]->getGvalue()) {
openTable[flag]->setGvalue(successor->getGvalue());
openTable[flag]->parente = bestPicture;
openTable[flag]->updateFvalue();
delete successor;
}
}
flag = inVector(closeTable, *successor);
if (flag >= 0) {
if (successor->getGvalue() < closeTable[flag]->getGvalue()) {
closeTable[flag]->setGvalue(successor->getGvalue());
closeTable[flag]->parente = bestPicture;
closeTable[flag]->updateFvalue();
delete successor;
openQueue.push(closeTable[flag]);
openTable.push_back(closeTable[flag]);
closeTable.erase(closeTable.begin() + flag);
}
} else {
successor->setHValue(*endPicture);
successor->updateFvalue();
openQueue.push(successor);
openTable.push_back(successor);
}
}
}
}
return false;
}

int Picture::rowSize = 0;
int Picture::columnSize = 0;

void showResult(const Picture *endPicture) {
if (endPicture != NULL) {
showResult(endPicture->parente);
endPicture->showPicture();
}
}

int main() {
int rowSize = 0, columnSize = 0;
cout << "Please input the size of the Picture" << endl;
cout << "Input the size of row" << endl;
cin >> rowSize;
cout << "Input the size of column" << endl;
cin >> columnSize;
Picture::setSize(rowSize, columnSize);
cout << "Please input the begin Picture" << endl;
Picture *beginPicture = new Picture();
cout << "Please input the end Picture" << endl;
Picture *endPicture = new Picture();
if (Astart(beginPicture, endPicture)) {
cout<<"The Route is as following"<<endl;
showResult(endPicture);
cout << "All steps is " << endPicture->getGvalue() << endl;
} else{
cout<<"This A start algorithm can not find the answer"<<endl;
}

}