-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource3.cpp
More file actions
279 lines (279 loc) · 6.18 KB
/
Copy pathSource3.cpp
File metadata and controls
279 lines (279 loc) · 6.18 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//#include <iostream>
//#include <stdlib.h>
//#include <vector>
//using namespace std;
//class Puzzle{
// int goal[3][3];
// int initial[3][3];
// int gn = 0;
// vector<int> queue;
//public:
// Puzzle(int p[][3], int s[][3]){
//
// for (int i = 0; i < 3; i++){
// for (int j = 0; j < 3; j++){
// this->goal[i][j] = p[i][j];
// }
// }
//
//
// for (int i = 0; i < 3; i++){
// for (int j = 0; j < 3; j++){
// this->initial[i][j] = s[i][j];
// }
// }
// }
// void increment_gn(){
// this->gn += 1;
// }
// void dump(){
// for (int i = 0; i < 3; i++){
// for (int j = 0; j < 3; j++){
// cout << this->initial[i][j] << " ";
// }
// cout << endl;
// }
// }
// bool isGoalState(){
// for (int i = 0; i < 3; i++){
// for (int j = 0; j < 3; j++){
// if (this->goal[i][j] != this->initial[i][j])
// return false;
// }
// }
// return true;
// }
// int hn(int temp[][3]){
// int counter = 0;
// //dump(temp);
// for (int i = 0; i < 3; i++){
// for (int j = 0; j < 3; j++){
// // && this->puzzle_goal[i][j] != 0
// if (temp[i][j] != this->goal[i][j] && temp[i][j] != 0){
// counter += 1;
// }
// }
// }
//
// return counter;
// }
// int up(){
// int temp[3][3], i, j;
//
// for (i = 0; i<3; i++) //menyalin array A dalam array untuk pengujian
// for (j = 0; j<3; j++)
// temp[i][j] = this->initial[i][j];
//
// for (i = 1; i<3; i++) // swapping the space in upward direction
// for (j = 0; j<3; j++)
// if (this->initial[i][j] == 0)
// {
// temp[i - 1][j] = 0;
// temp[i][j] = this->initial[i - 1][j];
// }
// return hn(temp);
// }
// int down(){
// int temp[3][3], i, j;
//
// for (i = 0; i<3; i++)
// for (j = 0; j<3; j++)
// temp[i][j] = this->initial[i][j];
//
// for (i = 0; i<2; i++) // swapping the space in downward direction
// for (j = 0; j<3; j++)
// if (this->initial[i][j] == 0)
// {
// temp[i + 1][j] = 0;
// temp[i][j] = this->initial[i + 1][j];
// }
// return hn(temp);
// }
// int left(){
// int temp[3][3], i, j;
//
// for (i = 0; i<3; i++)
// for (j = 0; j<3; j++)
// temp[i][j] = this->initial[i][j];
//
// for (i = 0; i<3; i++) // swapping the space in the left direction
// for (j = 1; j<3; j++)
// if (this->initial[i][j] == 0)
// {
// temp[i][j - 1] = 0;
// temp[i][j] = this->initial[i][j - 1];
// }
// return hn(temp);
// }
// int right(){
// int temp[3][3], i, j;
//
// for (i = 0; i<3; i++)
// for (j = 0; j<3; j++)
// temp[i][j] = this->initial[i][j];
//
// for (i = 0; i<3; i++) // swapping the space in the right direction
// for (j = 0; j<2; j++)
// if (this->initial[i][j] == 0)
// {
// temp[i][j + 1] = 0;
// temp[i][j] = this->initial[i][j + 1];
// }
// return hn(temp);
// }
// int getMin(){
// vector<int>::iterator it;
// int t = this->queue[0];
// it = this->queue.begin();
// this->queue.erase(it);
// return t;
//
// }
// void sortQueue(){
// bool swapp = true;
// while (swapp){
// swapp = false;
// for (size_t i = 0; i < this->queue.size() - 1; i++) {
// if (queue[i]>queue[i + 1]){
// queue[i] += queue[i + 1];
// queue[i + 1] = queue[i] - queue[i + 1];
// queue[i] -= queue[i + 1];
// swapp = true;
// }
// }
// }
//
// }
// int minimum(int a, int b, int c, int d){
// int min = a;
// if (b<min)
// min = b;
// if (c<min)
// min = c;
// if (d<min)
// min = d;
//
// return min;
// }
// void modify(int u, int d, int r, int l){
//
// int min = minimum(u, d, l, r);
// //printf("%d\n", min);
// char ran[4];
// int serial = 0;
// if (min == r)
// ran[serial++] = 'r';
// if (min == l)
// ran[serial++] = 'l';
// if (min == u)
// ran[serial++] = 'u';
// if (min == d)
// ran[serial++] = 'd';
//
// int sel = rand() % serial;
// char change = ran[sel]; int i, j;
// if (change == 'r')
// {
// for (i = 0; i<3; i++)
// for (j = 0; j<2; j++)
// if (initial[i][j] == 0) {
// initial[i][j] = initial[i][j + 1];
// initial[i][j + 1] = 0;
//
// }
// }
//
// else if (change == 'l')
// {
// for (i = 0; i<3; i++)
// for (j = 1; j<3; j++)
// if (initial[i][j] == 0) {
// initial[i][j] = initial[i][j - 1];
// initial[i][j - 1] = 0;
// }
// }
//
// else if (change == 'u')
// {
// for (i = 1; i<3; i++)
// for (j = 0; j<3; j++)
// if (initial[i][j] == 0){
// initial[i][j] = initial[i - 1][j];
// initial[i - 1][j] = 0;
//
// }
// }
//
// else if (change == 'd')
// {
// for (i = 0; i<2; i++)
// for (j = 0; j<3; j++)
// if (initial[i][j] == 0){
// initial[i][j] = initial[i + 1][j]; initial[i + 1][j] = 0;
// }
// }
// }
// int solver(){
//
// int gup, gdown, gleft, gright;
// int serial = 0;
// char ran[4];
//
// int steps = 1;
// this->queue.push_back(this->hn(this->initial) + this->gn);
// while (!this->queue.empty()){
//
// this->sortQueue();
// int m = getMin();
//
// if (isGoalState()){
//
// system("pause");
// system("cls");
// dump();
// cout << "Goal state Reached" << endl;
// cout << "Total steps : " << gn << endl;
// cout << "Total heuristic Cost : " << m << endl;
// break;
// }
//
// this->increment_gn();
//
// gup = this->up() + gn;
// gdown = this->down() + gn;
// gright = this->right() + gn;
// gleft = this->left() + gn;
//
// this->queue.push_back(gup);
// this->queue.push_back(gdown);
// this->queue.push_back(gleft);
// this->queue.push_back(gright);
//
// steps += 1;
// modify(gup, gdown, gright, gleft);
//
// cout << "Step No: " << steps << endl;
// dump();
// cout << endl;
//
// system("pause");
//
// }
// return 0;
// }
//};
//int main(){
//
// int puzzle_goal[3][3] = { { 1, 2, 3 }, { 8, 0, 4 }, { 7, 6, 5 } };
// // //int puzzle_search[3][3] = { { 1, 2, 3 }, { 8, 0, 4 }, { 7, 6, 5 } };
// int puzzle_search[3][3] = {
// { 2, 8, 3 },
// { 1, 6, 4 },
// { 7, 0, 5 }
// };
// Puzzle obj(puzzle_goal, puzzle_search);
// obj.solver();
// cout.put('\n');
//
//
//}