forked from takikhasan/Task
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN Puzzle Problem.cpp
More file actions
163 lines (158 loc) · 3.91 KB
/
N Puzzle Problem.cpp
File metadata and controls
163 lines (158 loc) · 3.91 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
#include<bits/stdc++.h>
using namespace std;
class Board
{
int n, g, h;
vector<vector<int>> B;
public:
Board()
{
}
Board(const Board& org)
{
n = org.n;
g = org.g;
h = org.h;
B = org.B;
}
Board(int nn, int gg, vector<vector<int>> BB)
{
n = nn, g = gg;
B = BB;
h = heuristic();
}
vector<Board> generateStates()
{
int r, c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (B[i][j] == 0) {
r = i;
c = j;
break;
}
}
}
vector<Board> ret;
Board temp = Board(n, g + 1, B);
if (r) {
int t = temp.B[r][c];
temp.B[r][c] = temp.B[r-1][c];
temp.B[r-1][c] = t;
ret.push_back(temp);
}
temp = Board(n, g + 1, B);
if (r != (n - 1)) {
int t = temp.B[r][c];
temp.B[r][c] = temp.B[r+1][c];
temp.B[r+1][c] = t;
ret.push_back(temp);
}
temp = Board(n, g + 1, B);
if (c) {
int t = temp.B[r][c];
temp.B[r][c] = temp.B[r][c-1];
temp.B[r][c-1] = t;
ret.push_back(temp);
}
temp = Board(n, g + 1, B);
if (c != (n - 1)) {
int t = temp.B[r][c];
temp.B[r][c] = temp.B[r][c+1];
temp.B[r][c+1] = t;
ret.push_back(temp);
}
return ret;
}
int heuristic()
{
int ret = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int d = i * n + j + 1;
if (d == (n * n)) d = 0;
if (B[i][j] != d) {
ret++;
}
}
}
return ret;
}
bool operator < (const Board& newBoard) const
{
int h1 = g + h;
int h2 = newBoard.g + newBoard.h;
if (h1 < h2)
return 1;
else if (h1 > 2)
return 0;
else
return B < newBoard.B;
}
bool operator == (const Board& newBoard) const
{
return (*this) < newBoard;
}
Board& operator = (const Board& org)
{
n = org.n;
g = org.g;
h = org.h;
B = org.B;
return *this;
}
};
void A_Star(Board start, Board goal)
{
set<Board> openList, closedList;
map<Board, Board> par;
openList.insert(start);
while (openList.size()) {
Board current = *(openList.begin());
openList.erase(openList.begin());
closedList.insert(current);
vector<Board> newStates = current.generateStates();
for (Board newState: newStates) {
if (closedList.find(newState) == closedList.end()) {
auto it = openList.find(newState);
if (it != openList.end()) {
if (newState < *it) {
openList.erase(it);
openList.insert(newState);
par.insert(make_pair(newState, current));
}
}
else {
openList.insert(newState);
par.insert(make_pair(newState, current));
}
}
}
}
vector<Board> ans;
while (1) {
ans.push_back(goal);
if (goal == start)
break;
else
goal = par[goal];
}
reverse(ans.begin(), ans.end());
}
int main()
{
cout << "Enter dimension of the puzzle (N * N) : ";
int n;
cin >> n;
vector<vector<int>> B(n);
cout << "Enter the puzzle (0 represents starting pos): " << endl;
for (int i = 0; i < n; i++) {
B[i].resize(4);
for (int j = 0; j < n; j++) {
cin >> B[i][j];
}
}
Board start(n, 0, B);
A_Star(start, start);
return 0;
}