-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle.cpp
More file actions
200 lines (161 loc) · 3.67 KB
/
puzzle.cpp
File metadata and controls
200 lines (161 loc) · 3.67 KB
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
#define PUZZLE_NUM 9
using namespace std;
typedef struct node{
char state[ PUZZLE_NUM ];
int f;
int g;
bool operator<(const node &o) const{
return f < o.f;
}
}NODE;
NODE start = {{2,8,3,1,6,4,7,-1,5},4};
NODE goal = {{1,2,3,8,-1,4,7,6,5},5};
int g = 0,stage = 0;
vector<NODE> open,close;
void draw(NODE &node);
void findChild();
int findEmpty();
int manhattan(NODE &node);
bool checkClose(char *node);
void swap(NODE &node, int empty, int point);
void drawClose();
int checkOpen(char *node);
void drawOpen();
void pushNode(NODE &node, char *state[PUZZLE_NUM], int empty, int point);
void draw(NODE &node){
for(int i = 0; i < PUZZLE_NUM; i++){
if(i%3 == 0)
printf("\n");
if(node.state[i] == -1)
printf(" ");
else
printf("%d ", node.state[i]);
}
printf("\n\nf(n) = %d and g(n) = %d of stage(%d)",node.f,g,stage);
printf("\n==================================\n");
}
void findChild(){
int empty = 0;
NODE tmpNode;
char *tmpState[PUZZLE_NUM];
memcpy(tmpState,open.front().state,9);
empty = findEmpty();
close.push_back(open.front());
open.erase(open.begin());
g++;
if( empty != -1){
if((empty % 3) != 0){
pushNode(tmpNode,tmpState,empty,empty-1);
}
if(empty > 2){
pushNode(tmpNode,tmpState,empty,empty-3);
}
if((empty % 3) != 2){
pushNode(tmpNode,tmpState,empty,empty +1);
}
if(empty < 6){
pushNode(tmpNode,tmpState,empty,empty +3);
}
}
sort(open.begin(),open.end());
return;
}
void pushNode(NODE &node, char *state[PUZZLE_NUM], int empty, int point){
memcpy(node.state,state,9);
swap(node,empty,point);
node.g = g;
node.f = manhattan(node);
if(checkOpen(node.state) != 0)
open[checkOpen(node.state)].f = node.f;
else if(checkClose(node.state))
open.push_back(node);
}
void swap(NODE &node, int empty, int point){
int tmp = node.state[empty];
node.state[empty] = node.state[point];
node.state[point] = tmp;
}
int findEmpty(){
for(int i = 0; i < PUZZLE_NUM; i++){
if(open.front().state[i] == -1)
return i;
}
return -1;
}
int manhattan(NODE &node){
int cmp = 0;
char *tmpStr[PUZZLE_NUM];
memcpy(tmpStr,goal.state,9);
for(int i = 0; i < PUZZLE_NUM; i++){
if(node.state[i] != -1 && memcmp(node.state+i,goal.state+i,1) !=0)
cmp += 1;
}
return cmp + node.g;
}
bool checkClose(char *node){
for(int i = 0; i < close.size(); i++){
if(memcmp(node,close[i].state,9) == 0)
return false;
}
return true;
}
int checkOpen(char *node){
for(int i = 0; i < open.size(); i++){
if(memcmp(node,open[i].state,9) == 0)
return i;
}
return 0;
}
void drawClose(){
printf("Current closed queue state is \n");
for(int i = 0; i < close.size(); i++){
for(int j = 0; j < PUZZLE_NUM; j++){
if(j%3 == 0)
printf("\n");
if(close[i].state[j] == -1)
printf(" ");
else
printf("%d ", close[i].state[j]);
}
printf("\n\nf(n) = %d and g(n) = %d\n",close[i].f,close[i].g);
}
}
void drawOpen(){
printf("Current opened queue state is \n");
for(int i = 0; i < open.size(); i++){
for(int j = 0; j < PUZZLE_NUM; j++){
if(j%3 == 0)
printf("\n");
if(open[i].state[j] == -1)
printf(" ");
else
printf("%d ", open[i].state[j]);
}
printf("\n\nf(n) = %d and g(n) = %d\n",open[i].f,open[i].g);
}
}
int main(){
open.push_back(start);
printf("Start puzzle's state is \n");
draw(open.front());
while(!open.empty()){
if(memcmp(open.front().state, goal.state, 9) == 0){
printf("Found Goal!\n");
draw(open.front());
printf("End Program\n");
break;
}
else{
findChild();
stage++;
draw(open.front());
g = open.front().g;
}
}
return 0;
}