-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.cpp
More file actions
617 lines (575 loc) · 15.7 KB
/
Copy pathgraph.cpp
File metadata and controls
617 lines (575 loc) · 15.7 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include <iostream>
#include <cstdio>
#include <cstring>
#include <Eigen/Dense>
#include <chrono>
#include "puzzle/puzzle_logic.hpp"
#include "puzzle/graph_logic.hpp"
bool clear(const Matrix54i &puzzle)
{
if (puzzle(4, 1) == 0 && puzzle(4, 2) == 0)
{
return true;
}
else
{
return false;
}
}
Matrix54i num_simple(const Matrix54i &puzzle, const int i)
{
Matrix54i copy_puzzle = puzzle;
std::vector<find_location_return> num = find_location(puzzle, i);
for (const auto &item : num)
{
if (item.val > 0 && item.val < 5)
{
copy_puzzle(item.y, item.x) = 2;
}
else if (item.val > 5 && item.val < 10)
{
copy_puzzle(item.y, item.x) = 3;
}
else
{
continue;
}
}
return copy_puzzle;
}
Matrix54i board_simple(const Matrix54i &puzzle)
{
Matrix54i copy_puzzle = puzzle;
for (int i = 1; i < 10; i++)
{
copy_puzzle = num_simple(copy_puzzle, i);
}
return copy_puzzle;
}
bool range_and_emptycheck_right(const Matrix54i &puzzle, const class find_location_return &ret)
{
if (inrange_check_right(ret) && empty_check_right(puzzle, ret))
{
return true;
}
else
{
return false;
}
}
bool range_and_emptycheck_left(const Matrix54i &puzzle, const class find_location_return &ret)
{
if (inrange_check_left(ret) && empty_check_left(puzzle, ret))
{
return true;
}
else
{
return false;
}
}
bool range_and_emptycheck_up(const Matrix54i &puzzle, const class find_location_return &ret)
{
if (inrange_check_up(ret) && empty_check_up(puzzle, ret))
{
return true;
}
else
{
return false;
}
}
bool range_and_emptycheck_down(const Matrix54i &puzzle, const class find_location_return &ret)
{
if (inrange_check_down(ret) && empty_check_down(puzzle, ret))
{
return true;
}
else
{
return false;
}
}
bool check_rightside(const Matrix54i &puzzle, const int checkval)
{
std::vector<find_location_return> num = find_location(puzzle, checkval);
if (checkval > 0 && checkval < 5)
{
for (const auto &item : num)
{
if (range_and_emptycheck_right(puzzle, item) == false)
{
return false;
}
}
return true;
}
else if (checkval > 5 && checkval < 10)
{
return range_and_emptycheck_right(puzzle, num[0]);
}
else if (checkval == 5)
{
return range_and_emptycheck_right(puzzle, num[1]);
}
else if (checkval == 0)
{
if (range_and_emptycheck_right(puzzle, num[1]) && range_and_emptycheck_right(puzzle, num[3]))
{
return true;
}
return false;
}
else
{
return false;
}
return false;
}
bool check_leftside(const Matrix54i &puzzle, const int checkval)
{
std::vector<find_location_return> num = find_location(puzzle, checkval);
if (checkval > 0 && checkval < 5)
{
for (const auto &item : num)
{
if (range_and_emptycheck_left(puzzle, item) == false)
{
return false;
}
}
return true;
}
else if (checkval > 4 && checkval < 10)
{
return range_and_emptycheck_left(puzzle, num[0]);
}
else if (checkval == 0)
{
if (range_and_emptycheck_left(puzzle, num[0]) && range_and_emptycheck_left(puzzle, num[2]))
{
return true;
}
return false;
}
return false;
}
bool check_upside(const Matrix54i &puzzle, int checkval)
{
std::vector<find_location_return> num = find_location(puzzle, checkval);
if (checkval == 5)
{
for (const auto &item : num)
{
if (range_and_emptycheck_up(puzzle, item) == false)
{
return false;
}
}
return true;
}
else if (checkval > 0 && checkval < 5)
{
return range_and_emptycheck_up(puzzle, num[0]);
}
else if (checkval > 5 && checkval < 10)
{
return range_and_emptycheck_up(puzzle, num[0]);
}
else if (checkval == 0)
{
if (range_and_emptycheck_up(puzzle, num[0]) && range_and_emptycheck_up(puzzle, num[1]))
{
return true;
}
return false;
}
return false;
}
bool check_downside(const Matrix54i &puzzle, int checkval)
{
std::vector<find_location_return> num = find_location(puzzle, checkval);
if (checkval == 5)
{
for (const auto &item : num)
{
if (range_and_emptycheck_down(puzzle, item) == false)
{
return false;
}
}
return true;
}
else if (checkval > 0 && checkval < 5)
{
return range_and_emptycheck_down(puzzle, num[1]);
}
else if (checkval > 5 && checkval < 10)
{
return range_and_emptycheck_down(puzzle, num[0]);
}
else if (checkval == 0)
{
if (range_and_emptycheck_down(puzzle, num[2]) && range_and_emptycheck_down(puzzle, num[3]))
{
return true;
}
return false;
}
return false;
}
std::vector<int> numbers_canmove_right(const Matrix54i &puzzle)
{
std::vector<int> stack_moveright;
for (int i = 0; i < 10; i++)
{
if (check_rightside(puzzle, i))
{
stack_moveright.push_back(i);
}
}
return stack_moveright;
}
std::vector<int> numbers_canmove_left(const Matrix54i &puzzle)
{
std::vector<int> stack_moveleft;
for (int i = 0; i < 10; i++)
{
if (check_leftside(puzzle, i))
{
stack_moveleft.push_back(i);
}
}
return stack_moveleft;
}
std::vector<int> numbers_canmove_up(const Matrix54i &puzzle)
{
std::vector<int> stack_moveup;
for (int i = 0; i < 10; i++)
{
if (check_upside(puzzle, i))
{
stack_moveup.push_back(i);
}
}
return stack_moveup;
}
std::vector<int> numbers_canmove_down(const Matrix54i &puzzle)
{
std::vector<int> stack_movedown;
for (int i = 0; i < 10; i++)
{
if (check_downside(puzzle, i))
{
stack_movedown.push_back(i);
}
}
return stack_movedown;
}
Matrix54i movepuzzle_right(const Matrix54i &puzzle, const int val)
{
Matrix54i copy_puzzle = puzzle;
if (val == 0)
{
std::vector<find_location_return> num = find_location(puzzle, 0);
move_2x2_right(copy_puzzle, num[0], num[1], num[2], num[3]);
return copy_puzzle;
}
else if (val > 0 && val < 6)
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_2x1_to_right(copy_puzzle, num[0], num[1]);
return copy_puzzle;
}
else
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_1x1_to_right(copy_puzzle, num[0]);
return copy_puzzle;
}
}
Matrix54i movepuzzle_left(const Matrix54i &puzzle, const int val)
{
Matrix54i copy_puzzle = puzzle;
if (val == 0)
{
std::vector<find_location_return> num = find_location(puzzle, 0);
move_2x2_left(copy_puzzle, num[0], num[1], num[2], num[3]);
return copy_puzzle;
}
else if (val > 0 && val < 6)
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_2x1_to_left(copy_puzzle, num[0], num[1]);
return copy_puzzle;
}
else
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_1x1_to_left(copy_puzzle, num[0]);
return copy_puzzle;
}
}
Matrix54i movepuzzle_up(const Matrix54i &puzzle, const int val)
{
Matrix54i copy_puzzle = puzzle;
if (val == 0)
{
std::vector<find_location_return> num = find_location(puzzle, 0);
move_2x2_up(copy_puzzle, num[0], num[1], num[2], num[3]);
return copy_puzzle;
}
else if (val > 0 && val < 6)
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_2x1_to_up(copy_puzzle, num[0], num[1]);
return copy_puzzle;
}
else
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_1x1_to_up(copy_puzzle, num[0]);
return copy_puzzle;
}
}
Matrix54i movepuzzle_down(const Matrix54i &puzzle, const int val)
{
Matrix54i copy_puzzle = puzzle;
if (val == 0)
{
std::vector<find_location_return> num = find_location(puzzle, 0);
move_2x2_down(copy_puzzle, num[0],
num[1], num[2], num[3]);
return copy_puzzle;
}
else if (val > 0 && val < 6)
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_2x1_to_down(copy_puzzle, num[0], num[1]);
return copy_puzzle;
}
else
{
std::vector<find_location_return> num = find_location(puzzle, val);
move_1x1_to_down(copy_puzzle, num[0]);
return copy_puzzle;
}
}
std::vector<Matrix54i> move_right(const Matrix54i &puzzle)
{
std::vector<Matrix54i> ret_list;
std::vector<int> right_movenum = numbers_canmove_right(puzzle);
for (const auto &right_num : right_movenum)
{
Matrix54i next_puzzle = movepuzzle_right(puzzle, right_num);
ret_list.push_back(next_puzzle);
}
return ret_list;
}
std::vector<Matrix54i> move_left(const Matrix54i &puzzle)
{
std::vector<Matrix54i> ret_list;
std::vector<int> left_movenum = numbers_canmove_left(puzzle);
for (const auto &left_num : left_movenum)
{
Matrix54i next_puzzle = movepuzzle_left(puzzle, left_num);
ret_list.push_back(next_puzzle);
}
return ret_list;
}
std::vector<Matrix54i> move_up(const Matrix54i &puzzle)
{
std::vector<Matrix54i> ret_list;
std::vector<int> up_movenum = numbers_canmove_up(puzzle);
for (const auto &up_num : up_movenum)
{
Matrix54i next_puzzle = movepuzzle_up(puzzle, up_num);
ret_list.push_back(next_puzzle);
}
return ret_list;
}
std::vector<Matrix54i> move_down(const Matrix54i &puzzle)
{
std::vector<Matrix54i> ret_list;
std::vector<int> down_movenum = numbers_canmove_down(puzzle);
for (const auto &down_num : down_movenum)
{
Matrix54i next_puzzle = movepuzzle_down(puzzle, down_num);
ret_list.push_back(next_puzzle);
}
return ret_list;
}
std::vector<Matrix54i> moved_board_list(const Matrix54i &input)
{
Matrix54i puzzle = input;
std::vector<Matrix54i> movable;
std::vector<Matrix54i> right = move_right(puzzle);
std::vector<Matrix54i> left = move_left(puzzle);
std::vector<Matrix54i> up = move_up(puzzle);
std::vector<Matrix54i> down = move_down(puzzle);
if (right.size() == 1)
{
movable.push_back(right[0]);
}
else if (right.size() == 2)
{
movable.push_back(right[0]);
movable.push_back(right[1]);
}
if (left.size() == 1)
{
movable.push_back(left[0]);
}
else if (left.size() == 2)
{
movable.push_back(left[0]);
movable.push_back(left[1]);
}
if (up.size() == 1)
{
movable.push_back(up[0]);
}
else if (up.size() == 2)
{
movable.push_back(up[0]);
movable.push_back(up[1]);
}
if (down.size() == 1)
{
movable.push_back(down[0]);
}
else if (down.size() == 2)
{
movable.push_back(down[0]);
movable.push_back(down[1]);
}
return movable;
}
std::string mat_to_str(const Matrix54i &simple_puzzle)
{
std::string hash;
int rows = simple_puzzle.rows();
int cols = simple_puzzle.cols();
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
int num = simple_puzzle(y, x);
std::string str = std::to_string(num);
hash += str;
}
}
return hash;
}
std::string mat_to_str_pluscomma(const Matrix54i &simple_puzzle)
{
std::string hash;
for (int y = 0; y < 5; y++)
{
for (int x = 0; x < 4; x++)
{
int num = simple_puzzle(y, x);
std::string str = std::to_string(num);
hash += str;
hash += ",";
}
}
return hash;
}
bool exist(const std::string &hash_puzzle,
std::vector<std::string> &puzzle_state)
{
std::chrono::system_clock::time_point start, end; // 計測用関数
start = std::chrono::system_clock::now();
// std::reverse(puzzle_state.begin(), puzzle_state.end());
if (std::find(puzzle_state.begin(), puzzle_state.end(),
hash_puzzle) == puzzle_state.end())
{
end = std::chrono::system_clock::now();
double time = static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
// printf("%.0lf\n", time);
return true;
}
else
{
end = std::chrono::system_clock::now();
double time = static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
// printf("%.0lf\n", time);
return false;
}
}
std::vector<Matrix54i> clearroute(std::unordered_map<Matrix54i, Matrix54i,
KeyHasher, KeyEqual> &edges,
const Matrix54i &moved_puzzle)
{
std::chrono::system_clock::time_point start, end; // 計測用関数
Matrix54i root = init_puzzle();
std::vector<Matrix54i> c_route;
Matrix54i val = moved_puzzle;
start = std::chrono::system_clock::now();
while (val != root)
{
c_route.push_back(val);
val = edges[val];
}
c_route.push_back(root);
return c_route;
}
void queue_state_append(const Matrix54i &before_moving, std::vector<Matrix54i> &movable,
std::vector<std::string> &puzzle_state,
std::unordered_map<Matrix54i, Matrix54i, KeyHasher, KeyEqual> &edges,
std::queue<Matrix54i> &puzzle_list,
std::vector<std::vector<Matrix54i>> &c_route)
{
for (Matrix54i &state : movable)
{
Matrix54i state_copy = state;
Matrix54i simple_puzzle = board_simple(state_copy);
std::string hash_puzzle = mat_to_str(simple_puzzle);
if (exist(hash_puzzle, puzzle_state))
{
edges[state] = before_moving;
puzzle_state.push_back(hash_puzzle);
puzzle_list.push(state);
if (clear(state))
{
std::vector<Matrix54i> add_c_route = clearroute(edges, state);
}
}
}
}
std::vector<std::vector<Matrix54i>> breadth_first_search(const Matrix54i &puzzle)
{
std::vector<std::vector<Matrix54i>> c_route;
std::unordered_map<Matrix54i, Matrix54i, KeyHasher, KeyEqual> edges;
std::vector<std::string> puzzle_state;
std::queue<Matrix54i> puzzle_list;
puzzle_list.push(puzzle);
Matrix54i simple_puzzle = board_simple(puzzle);
std::string str = mat_to_str(simple_puzzle);
puzzle_state.push_back(str);
while (!puzzle_list.empty())
{
Matrix54i now_puzzle = puzzle_list.front();
puzzle_list.pop();
std::vector<Matrix54i> movable = moved_board_list(now_puzzle);
queue_state_append(now_puzzle, movable,
puzzle_state, edges, puzzle_list, c_route);
}
std::cout << "総手数は" << puzzle_state.size() << "手です" << std::endl;
return c_route;
}
std::vector<Matrix54i> shortestroute_find(const std::vector<std::vector<Matrix54i>> &c_route)
{
std::vector<Matrix54i> shortest_vec;
bool isFirst = true;
for (std::vector<Matrix54i> v : c_route)
{
if (isFirst || v.size() < shortest_vec.size())
{
shortest_vec = v;
isFirst = false;
}
}
return shortest_vec;
}