-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (164 loc) · 3.89 KB
/
main.cpp
File metadata and controls
186 lines (164 loc) · 3.89 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
#include "PegSolitaire.hpp"
#include <iostream>
#include <chrono>
#include <algorithm>
vector<Direction> ps::directions { Direction(0, 1), Direction(0, -1), Direction(-1, 0), Direction(1, 0) };
size_t ps::count_pegs(board english_board)
{
size_t sum = 0;
for (auto& v : english_board) {
sum += count(v.begin(), v.end(), '1');
}
return (sum);
}
board ps::get_english_board()
{
auto english_board = board(board_size, vector<char>(board_size, '1'));
english_board.at(middle_index).at(middle_index) = '0';
for (int index = 0; index < 2; index++) {
int reverse_index = board_size - 1 - index;
for (int inner_index = 0; inner_index < 2; inner_index++) {
int reverse_inner_index = board_size - 1 - inner_index;
english_board.at(index).at(inner_index) = ' ';
english_board.at(index).at(reverse_inner_index) = ' ';
english_board.at(reverse_index).at(reverse_inner_index) = ' ';
english_board.at(reverse_index).at(inner_index) = ' ';
}
}
return (english_board);
}
void ps::print(board english_board)
{
for_each(english_board.begin(), english_board.end(), [](vector<char> line) {
for_each(line.begin(), line.end(), [](char c) {
cout << c;
});
cout << "\n";
});
}
vector<board> ps::solve(board english_board)
{
vector<board> moves;
bool no_solution = true;
size_t x_size = english_board.size();
for (int idx = 0; idx < x_size; idx++) {
size_t y_size = english_board[idx].size();
for (int idy = 0; idy < y_size; idy++) {
if (english_board[idy][idx] == '1') {
for (auto& direction : directions) {
int posx = idx + direction.X();
int posy = idy + direction.Y();
int posxx = posx + direction.X();
int posyy = posy + direction.Y();
if ((posx >= 0) && (posy >= 0) && (posxx >= 0) && (posyy >= 0) && (posxx < x_size) && (posyy < y_size)) {
if ((english_board[posy][posx] == '1') && (english_board[posyy][posxx] == '0')) {
no_solution = false;
english_board[idy][idx] = '0';
english_board[posy][posx] = '0';
english_board[posyy][posxx] = '1';
/*print(english_board);
std::cout << "---------------\n";*/
moves = solve(english_board);
if (moves.size() == 0) {
english_board[idy][idx] = '1';
english_board[posy][posx] = '1';
english_board[posyy][posxx] = '0';
/*print(english_board);
std::cout << "---------------\n";*/
}
else {
if (count_pegs(english_board) > 1) {
moves.insert(moves.begin(), english_board);
}
idx = x_size;
idy = y_size;
}
}
}
}
}
}
}
if (no_solution)
{
if ((count_pegs(english_board) == 1) && (english_board[middle_index][middle_index] == '1')) {
moves.push_back(english_board);
}
}
return (moves);
}
void ps::print_moves(vector<board> results)
{
for (auto& v : results) {
print(v);
std::cout << "---------------\n";
}
}
int main()
{
using namespace std::chrono;
auto const english_board = ps::get_english_board();
std::cout << "Start solving:\n";
ps::print(english_board);
std::cout << "---------------\n";
auto const start = high_resolution_clock::now();
// Solving board e.g.
//
// Add your code
//
// ps::solve(english_board, moves); or
const auto moves = ps::solve(english_board);
//ps::test(english_board);
auto end = high_resolution_clock::now();
auto duration_ms = duration_cast<duration<double, std::milli>>(end - start).count();
// Print moves
//
// Add your code
// e.g.
ps::print_moves(moves);
std::cout << "Solving took: " << duration_ms << " ms\n";
/*
Output example:
~~~~~~~~~~~~~~~~
Start solving:
111
111
1111111
1110111
1111111
111
111
---------------
111
111
1111111
1110111
1111111
111
111
111
111
1111111
1111001
1111111
111
111
[ ... omitted ...]
000
000
0000000
0000000
0001000
010
000
000
000
0000000
0000000
0000000
000
010
Solving took: 492.057 ms
*/
return 0;
}