-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedy Search Fazeel.cpp
228 lines (202 loc) · 5.13 KB
/
Greedy Search Fazeel.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
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
/*
Name Muhammad Fazeel
Reg No 453385
AI Assignment 2
*/
#include <bits/stdc++.h>
using namespace std;
map<vector<vector<int> >, bool> visited;
map<vector<vector<int> >, vector<vector<int> > > parent;
vector<vector<int> > goal(3, vector<int>(3));
bool visit(vector<vector<int> > a)
{
if (visited[a] == true)
return true;
else
return false;
}
int heuristic(vector<vector<int> > a, int choice)
{
if (choice == 1) // Manhattan Distance
{
int dist = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] != 0)
{
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
if (a[i][j] == goal[k][l])
dist += abs(i - k) + abs(j - l);
}
}
}
}
}
return dist;
}
else if (choice == 2) // Misplaced Tiles
{
int misplaced = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] != goal[i][j])
misplaced++;
}
}
return misplaced;
}
return 0; // Default case (shouldn't happen)
}
bool isGoal(vector<vector<int> > a)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] != goal[i][j])
return false;
}
}
return true;
}
bool safe(int i, int j)
{
if (i >= 0 && i <= 2 && j >= 0 && j <= 2)
return true;
else
return false;
}
int dx[] = {-1, +1, 0, 0};
int dy[] = {0, 0, -1, +1};
vector<vector<vector<int> > > neighbours(vector<vector<int> > a)
{
pair<int, int> pos;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] == 0)
{
pos.first = i;
pos.second = j;
break;
}
}
}
vector<vector<vector<int> > > ans;
for (int k = 0; k < 4; k++)
{
int cx = pos.first;
int cy = pos.second;
vector<vector<int> > n = a;
if (safe(cx + dx[k], cy + dy[k]))
{
swap(n[cx + dx[k]][cy + dy[k]], n[cx][cy]);
ans.push_back(n);
}
}
return ans;
}
typedef pair<vector<vector<int> >, int> state;
struct cmp
{
bool operator()(state &a, state &b)
{
return a.second > b.second; // Changed to use greater than for priority queue
}
};
void print(vector<vector<int> > s)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", s[i][j]);
}
cout << endl;
}
}
void print_path(vector<vector<int> > s, std::ofstream &outFile)
{
if (parent.count(s))
print_path(parent[s], outFile);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
outFile << s[i][j] << " ";
}
outFile << endl;
}
outFile << endl;
}
void solve(vector<vector<int> > a, int moves, int heuristicChoice, ofstream &outFile)
{
priority_queue<state, vector<state>, cmp> Q;
Q.push(state(a, 0));
while (!Q.empty())
{
vector<vector<int> > s = Q.top().first;
moves = Q.top().second; // Corrected to get the moves from the priority queue
Q.pop();
visited[s] = true;
if (s == goal)
{
print_path(s, outFile);
break;
}
vector<vector<vector<int> > > ns = neighbours(s);
vector<vector<vector<int> > >::iterator it;
for (it = ns.begin(); it != ns.end(); it++)
{
vector<vector<int> > temp = *it;
if (!visit(temp))
{
parent.insert(pair<vector<vector<int> >, vector<vector<int> > >(temp, s));
int h = heuristic(temp, heuristicChoice);
Q.push(state(temp,h));
}
}
}
return;
}
int main()
{
vector<vector<int> > a(3, vector<int>(3));
cout << "Enter the initial state:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> a[i][j];
}
}
int heuristicChoice;
cout << "Choose heuristic:\n1. Manhattan Distance\n2. Misplaced Tiles\n";
cin >> heuristicChoice;
goal[0][0] = 0;
goal[0][1] = 1;
goal[0][2] = 2;
goal[1][0] = 3;
goal[1][1] = 4;
goal[1][2] = 5;
goal[2][0] = 6;
goal[2][1] = 7;
goal[2][2] = 8;
ofstream outFile("Greedy Search solution Assignment 2.txt");
if (!outFile.is_open())
{
cerr << "Unable to open the file for writing." << endl;
return 1; // Exit with an error code
}
cout << "Solution...\n\n";
solve(a, 0, heuristicChoice, outFile);
outFile.close();
}