-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkod.cpp
349 lines (307 loc) · 9.38 KB
/
kod.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> ii;
const int MAXN = 22;
const int MAX_STATE_ID = 20203;
int N,M;
int INITIAL_STATE_ID, GOAL_STATE_ID;
int explored_num, max_depth;
int DIRS[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int cost[MAX_STATE_ID];
int last_move[MAX_STATE_ID];
int depth[MAX_STATE_ID];
bool used[MAX_STATE_ID];
bool explored[MAX_STATE_ID];
char ar[MAXN][MAXN],c;
queue<int> q;
vector<char> output;
priority_queue<ii> heap;
// Getting the input and creating the array.
// Also calculates the initial and goal state ids.
string init(char *argv1, char *argv2){
freopen(argv1, "r", stdin);
cin >> M >> N;
string str;
for(int i=0;i<=N;i++){
getline(cin, str);
for(int j=0;j<str.length();j++)
ar[i][j+1]=str.at(j);
}
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if(ar[i][j]=='s')
INITIAL_STATE_ID=i*1000+j*10;
else if(ar[i][j]=='g')
GOAL_STATE_ID = i*1000+j*10;
return argv2;
}
// Returns true if the given cell is available, false otherwise.
bool is_available(int x, int y){
if(x<1 || x>N || y<1 || y>M)
return false;
return ar[x][y]!=' ';
}
// Is the state that I'll go valid?
// Caller of is_available
bool is_valid(int state){
int x=state/1000;
int y=(state/10)%100;
if(!is_available(x, y)) return false;
if(state%10==0) return true;
if(state%10==1) return is_available(x+DIRS[2][0], y+DIRS[2][1]);
return is_available(x+DIRS[1][0], y+DIRS[1][1]);
}
// Absolute value
int abs(int x){
return x<0?-x:x;
}
// This function generates the next state id for a given state and direction.
int find_state_id(int state, int dir){
int next_state = -1;
if(state%10==0)
switch(dir){
case 0: next_state = state-19; break;
case 1: next_state = state-998; break;
case 2: next_state = state+11; break;
case 3: next_state = state+2002; break;
}
else if(state%10==1)
switch(dir){
case 0: next_state = state-11; break;
case 1: next_state = state-1000; break;
case 2: next_state = state+19; break;
case 3: next_state = state+1000;break;
}
else
switch(dir){
case 0: next_state = state-10; break;
case 1: next_state = state-2002; break;
case 2: next_state = state+10; break;
case 3: next_state = state+998; break;
}
return is_valid(next_state)?next_state:-1;
}
// I used this in order to find the solution path.
// This function finds the parent state for a given state and the last direction to come at that state.
int get_parent(int state, int dir){
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
for(int k=0;k<=2;k++)
if(find_state_id(i*1000+j*10+k, dir)==state)
return i*1000+j*10+k;
}
// Calculates the cost of the move based on the project description.
int calculate_cost(int state, int dir){
if(state%10==1 && (dir==0 || dir==2)) return 3;
if(state%10==2 && (dir==1 || dir==3)) return 3;
return 1;
}
// Converts direction id into direction letter.
char to_direction(int direction_id){
switch(direction_id){
case 0: return 'L';
case 1: return 'U';
case 2: return 'R';
case 3: return 'D';
}
}
// Manhattan distance + orientation check
int heuristic(int state){
return abs((state/10)%100-(GOAL_STATE_ID/10)%100)+abs(state/1000-GOAL_STATE_ID/1000)+(state%10?1:0);
}
// Classical DFS algorithm.
// Goes wherever it can go, unless the state is visited before.
void dfs(int state, int depth, int cost, vector<int> moves){
explored[state]=true;
if(depth>max_depth) max_depth=depth;
if(state==GOAL_STATE_ID){
cout << cost << " " << explored_num << " " << max_depth << " " << moves.size() << endl;
for(int i=0;i<moves.size();i++) cout << to_direction(moves[i]);
return;
}
explored_num++;
for(int i=0;i<4;i++){
int next_state = find_state_id(state, i);
if(next_state==-1) continue;
if(explored[next_state]) continue;
moves.push_back(i);
dfs(next_state, depth+1, cost+calculate_cost(state, i), moves);
moves.pop_back();
}
}
// I push all new states to a queue.
// Finds the solution by picking the nodes one by one.
// Classical BFS algorithm, not optimal, but good.
void bfs(int state){
explored[state]=true;
q.push(state);
while(!q.empty()){
state = q.front();
q.pop();
explored_num++;
for(int i=0;i<4;i++){
int next_state = find_state_id(state, i);
if(next_state==-1) continue;
if(explored[next_state]) continue;
explored[next_state]=true;
depth[next_state]=depth[state]+1;
cost[next_state]=cost[state]+calculate_cost(state, i);
last_move[next_state]=i;
q.push(next_state);
if(next_state==GOAL_STATE_ID){
state=next_state;
cout << cost[state] << " " << explored_num << " " << depth[state] << " " << depth[state] << endl;
while(state!=INITIAL_STATE_ID){
output.push_back(to_direction(last_move[state]));
state=get_parent(state, last_move[state]);
}
while(output.size()>0){
cout << output[output.size()-1];
output.pop_back();
}
return;
}
}
}
}
// f()=g()
// I push all new states to a priority queue with their f() values.
// Finds the solution by picking the nodes with the smallest f() one by one.
// It seemed similar to Dijkstra's algorithm for me. Always finds the optimal solution.
void ucs(int state){
explored[state]=true;
heap.push(ii(0, state));
while(!heap.empty()){
state=heap.top().se;
heap.pop();
while(!heap.empty() && used[state]){
state=heap.top().se;
heap.pop();
}
if(heap.empty() && used[state]) return;
used[state]=true;
if(depth[state]>max_depth) max_depth=depth[state];
explored_num++;
for(int i=0;i<4;i++){
int next_state = find_state_id(state, i);
if(next_state==-1) continue;
if(explored[next_state] && cost[next_state]<=cost[state]+calculate_cost(state,i)) continue;
explored[next_state]=true;
depth[next_state]=depth[state]+1;
cost[next_state]=cost[state]+calculate_cost(state, i);
last_move[next_state]=i;
heap.push(ii(-cost[next_state], next_state));
if(next_state==GOAL_STATE_ID){
state=next_state;
cout << cost[state] << " " << explored_num << " " << max(max_depth, depth[state]) << " " << depth[state] << endl;
while(state!=INITIAL_STATE_ID){
output.push_back(to_direction(last_move[state]));
state=get_parent(state, last_move[state]);
}
while(output.size()>0){
cout << output[output.size()-1];
output.pop_back();
}
return;
}
}
}
}
// f()=h'()
// I push all new states to a priority queue with their f() values.
// Finds the solution by picking the nodes with the smallest f() one by one.
// It always picks the node with the smallest heuristic cost.
void greedy(int state){
explored[state]=true;
heap.push(ii(heuristic(state), state));
while(!heap.empty()){
state=heap.top().se;
heap.pop();
if(depth[state]>max_depth) max_depth=depth[state];
explored_num++;
for(int i=0;i<4;i++){
int next_state = find_state_id(state, i);
if(next_state==-1) continue;
if(explored[next_state]) continue;
explored[next_state]=true;
depth[next_state]=depth[state]+1;
cost[next_state]=cost[state]+calculate_cost(state, i);
last_move[next_state]=i;
heap.push(ii(-heuristic(next_state), next_state));
if(next_state==GOAL_STATE_ID){
state=next_state;
cout << cost[state] << " " << explored_num << " " << max(max_depth, depth[state]) << " " << depth[state] << endl;
while(state!=INITIAL_STATE_ID){
output.push_back(to_direction(last_move[state]));
state=get_parent(state, last_move[state]);
}
while(output.size()>0){
cout << output[output.size()-1];
output.pop_back();
}
return;
}
}
}
}
// f()=g()+h'()
// I push all new states to a priority queue with their f() values.
// Finds the solution by picking the nodes with the smallest f() one by one.
// Of course, if another(smaller) f() can be found for a state, it's been fixed and re-pushed.
void a_star(int state){
explored[state]=true;
heap.push(ii(-heuristic(state), state));
while(!heap.empty()){
state=heap.top().se;
heap.pop();
while(!heap.empty() && used[state]){
state=heap.top().se;
heap.pop();
}
if(heap.empty() && used[state]) return;
used[state]=true;
if(depth[state]>max_depth)
max_depth=depth[state];
explored_num++;
for(int i=0;i<4;i++){
int next_state = find_state_id(state, i);
if(next_state==-1) continue;
if(explored[next_state] && cost[next_state]<cost[state]+calculate_cost(state,i)) continue;
explored[next_state]=true;
depth[next_state]=depth[state]+1;
cost[next_state]=cost[state]+calculate_cost(state, i);
last_move[next_state]=i;
heap.push(ii(-cost[next_state]-heuristic(next_state), next_state));
if(next_state==GOAL_STATE_ID){
state=next_state;
cout << cost[state] << " " << explored_num << " " << max(max_depth, depth[state]) << " " << depth[state] << endl;
while(state!=INITIAL_STATE_ID){
output.push_back(to_direction(last_move[state]));
state=get_parent(state, last_move[state]);
}
while(output.size()>0){
cout << output[output.size()-1];
output.pop_back();
}
return;
}
}
}
}
int main(int argc, char **argv){
string method = init(argv[1], argv[2]);
vector<int> empty_vector;
if(method=="dfs") dfs(INITIAL_STATE_ID, 0, 0, empty_vector);
else if(method=="bfs") bfs(INITIAL_STATE_ID);
else if(method=="ucs") ucs(INITIAL_STATE_ID);
else if(method=="gs") greedy(INITIAL_STATE_ID);
else if(method=="as") a_star(INITIAL_STATE_ID);
else cout << "WRONG ARGUMENT: "+method << endl;
return 0;
}