-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTilePuzzle-IDDFS.cpp
174 lines (165 loc) · 4.58 KB
/
TilePuzzle-IDDFS.cpp
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
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
typedef vector<vector<int>> Board;
struct point {
int x, y;
point(int X = 0, int Y = 0) {
x = X; y = Y;
}
};
int nodeIdCounter = 0;
//LCRS w/additional parent pointer.
struct node {
int ply;
Board board;
point blank;
node* parent;
node* next;
node* children;
node(Board info, point begin, node* parent, node* next) {
this->ply = 0;
this->blank.x = begin.x;
this->blank.y = begin.y;
this->board = info;
this->parent = parent;
this->next = next;
this->children = nullptr;
}
};
typedef node* link;
class SlidePuzzle {
bool found;
int M;
int tryCount = 0;
vector<point> dirs = {{1, 0}, {-1, 0}, {0,1},{0,-1}};
void printBoard(Board& board) {
for (vector<int> row : board) {
for (int col : row) {
cout<<col<<" ";
}
cout<<endl;
}
}
void showSolution(link h) {
link x = h;
link solution;
int moves = 0;
while (x != nullptr) {
printBoard(x->board);
cout<<"------\n";
x = x->parent;
moves++;
}
cout<<"Puzzle completed in "<<moves<<" moves after trying "<<tryCount<<" board configurations.\n";
}
bool isSafe(int x, int y) {
return x >= 0 && x < M && y >= 0 && y < M;
}
link generateChildren(link h) {
for (point d : dirs) {
point next = {h->blank.x+d.x, h->blank.y+d.y};
if (isSafe(next.x, next.y)) {
Board board = h->board;
swap(board[next.x][next.y], board[h->blank.x][h->blank.y]);
h->children = new node(board, next, h, h->children);
h->children->ply = h->ply + 1;
tryCount++;
}
}
return h->children;
}
point find(Board board, int x) {
for (int i = 0; i < M; i++) {
for (int j= 0; j < M; j++) {
if (x == board[i][j])
return point(i, j);
}
}
return point(-1,-1);
}
int getBoardCost(Board a, Board b) {
int cost = 0;
int oop = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (a[i][j] != b[i][j]) {
point p = find(b, a[i][j]);
cost += std::abs(i - p.x) + std::abs(j - p.y);
oop += 1;
}
}
}
return oop;
}
void cleanUp(link h) {
if (h != nullptr) {
cleanUp(h->children);
cleanUp(h->next);
delete h;
}
}
public:
SlidePuzzle() {
M = 3;
found = false;
}
void DLDFS(link curr, Board goal, int depth) {
if (depth >= 0) {
int cost = getBoardCost(curr->board, goal);
cout<<cost<<" ";
if (cost == 0) {
cout<<"Solution Found!\n";
showSolution(curr);
found = true;
return;
}
curr->children = generateChildren(curr);
for (link t = curr->children; t != nullptr; t = t->next) {
if (!found)
DLDFS(t, goal, depth-1);
}
}
}
void iddfs(Board first, Board goal) {
found = false;
tryCount = 0;
link start = new node(first, find(first, 0), nullptr, nullptr);
for (int i = 2; i < 20; i++) {
if (!found) {
cout<<"Trying with Depth Limit: "<<i<<"\n";
DLDFS(start, goal, i);
} else {
break;
}
}
cleanUp(start);
}
};
int main() {
Board p1start = {
{2,8,3},
{1,6,4},
{7,0,5}
};
Board p1goal = {
{1,2,3},
{8,0,4},
{7,6,5}
};
Board p2start = {
{1, 2, 3},
{5, 6, 0},
{7, 8, 4}
};
Board p2goal = {
{1, 2, 3},
{5, 8, 6},
{0, 7, 4}
};
SlidePuzzle ep;
//ep.iddfs(p1start, p1goal);
ep.iddfs(p2start, p2goal);
return 0;
}