File opdracht3/peg.cpp changed (mode: 100644) (index adedfd3..9ec5d42) |
... |
... |
typename std::vector<Vertex>::const_iterator Graph::cbegin(Vertex v) const |
8 |
8 |
{ |
{ |
9 |
9 |
adjacents.clear(); |
adjacents.clear(); |
10 |
10 |
|
|
11 |
|
for (int r=0; r<3; r++) { |
|
12 |
|
for (int c=0; c<3; c++) { |
|
|
11 |
|
for (int r=0; r<5; r++) { |
|
12 |
|
for (int c=0; c<r+1; c++) { |
13 |
13 |
if (v[r][c] == 0) { |
if (v[r][c] == 0) { |
14 |
|
if (c<1) adjacents.push_back(doMove(v,Move::l)); |
|
15 |
|
if (c>1) adjacents.push_back(doMove(v,Move::r)); |
|
16 |
|
if (r<1) adjacents.push_back(doMove(v,Move::u)); |
|
17 |
|
if (r>1) adjacents.push_back(doMove(v,Move::d)); |
|
|
14 |
|
if (r>=2) adjacents.push_back(doMove(v,Move::ld)); |
|
15 |
|
if (r>=2 && c>=2) adjacents.push_back(doMove(v,Move::rd)); |
|
16 |
|
if (r<=3) adjacents.push_back(doMove(v,Move::ru)); |
|
17 |
|
if (r<=3 && c<=3) adjacents.push_back(doMove(v,Move::lu)); |
18 |
18 |
} |
} |
19 |
19 |
} |
} |
20 |
20 |
} |
} |
|
... |
... |
Vertex doMove(const Vertex &v, const Move &m) |
31 |
31 |
bool done=false; |
bool done=false; |
32 |
32 |
Vertex n = v; |
Vertex n = v; |
33 |
33 |
|
|
34 |
|
for (int r=0; r<3 && !done; r++) { |
|
35 |
|
for (int c=0; c<3 && !done; c++) { |
|
|
34 |
|
for (int r=0; r<5 && !done; r++) { |
|
35 |
|
for (int c=0; c<r+1 && !done; c++) { |
36 |
36 |
if (v[r][c] == 0) { |
if (v[r][c] == 0) { |
37 |
37 |
switch(m) { |
switch(m) { |
38 |
|
case Move::l: |
|
39 |
|
n[r][c+1] = 0; |
|
40 |
|
std::swap(n[r][c], n[r][c+2]); |
|
|
38 |
|
case Move::ld: |
|
39 |
|
n[r-1][c] = 0; |
|
40 |
|
std::swap(n[r][c], n[r-2][c]); |
41 |
41 |
done = true; |
done = true; |
42 |
42 |
break; |
break; |
43 |
|
case Move::r: |
|
44 |
|
n[r][c-1] = 0; |
|
45 |
|
std::swap(n[r][c], n[r][c-2]); |
|
|
43 |
|
case Move::rd: |
|
44 |
|
n[r-1][c-1] = 0; |
|
45 |
|
std::swap(n[r][c], n[r-2][c-2]); |
46 |
46 |
done = true; |
done = true; |
47 |
47 |
break; |
break; |
48 |
|
case Move::u: |
|
|
48 |
|
case Move::ru: |
49 |
49 |
n[r+1][c] = 0; |
n[r+1][c] = 0; |
50 |
|
std::swap(n[r+2][c], n[r][c]); |
|
|
50 |
|
std::swap(n[r][c], n[r+2][c]); |
51 |
51 |
done = true; |
done = true; |
52 |
52 |
break; |
break; |
53 |
|
case Move::d: |
|
54 |
|
n[r-1][c] = 0; |
|
55 |
|
std::swap(n[r-2][c], n[r][c]); |
|
|
53 |
|
case Move::lu: |
|
54 |
|
n[r+1][c+1] = 0; |
|
55 |
|
std::swap(n[r][c], n[r+2][c+2]); |
56 |
56 |
done = true; |
done = true; |
57 |
57 |
break; |
break; |
58 |
58 |
} |
} |
|
... |
... |
Vertex doMove(const Vertex &v, const Move &m) |
64 |
64 |
|
|
65 |
65 |
std::ostream &operator<<(std::ostream &os, const Vertex &state) |
std::ostream &operator<<(std::ostream &os, const Vertex &state) |
66 |
66 |
{ |
{ |
67 |
|
for (int r=0; r<3; r++) { |
|
68 |
|
for (int c=0; c<3; c++) { |
|
|
67 |
|
for (int r=0; r<5; r++) { |
|
68 |
|
for (int x=4-r; x>0; x--) |
|
69 |
|
os << " "; |
|
70 |
|
for (int c=0; c<r+1; c++) { |
69 |
71 |
if (state[r][c] != 0) { |
if (state[r][c] != 0) { |
70 |
|
os << state[r][c]; |
|
|
72 |
|
os << "X "; |
71 |
73 |
} else { |
} else { |
72 |
|
os << " "; |
|
|
74 |
|
os << "O "; |
73 |
75 |
} |
} |
74 |
76 |
} |
} |
75 |
77 |
os << std::endl; |
os << std::endl; |
|
... |
... |
int main() |
144 |
146 |
Graph graph; |
Graph graph; |
145 |
147 |
|
|
146 |
148 |
Vertex start = {{ |
Vertex start = {{ |
147 |
|
{{1,2,3}}, |
|
148 |
|
{{0,7,4}}, |
|
149 |
|
{{8,6,5}} |
|
|
149 |
|
{{0}}, |
|
150 |
|
{{1,1}}, |
|
151 |
|
{{1,1,1}}, |
|
152 |
|
{{1,1,1,1}}, |
|
153 |
|
{{1,1,1,1,1}} |
150 |
154 |
}}; |
}}; |
151 |
155 |
|
|
152 |
156 |
Vertex goal = {{ |
Vertex goal = {{ |
153 |
|
{{1,2,3}}, |
|
154 |
|
{{4,0,0}}, |
|
155 |
|
{{8,6,5}} |
|
|
157 |
|
{{0}}, |
|
158 |
|
{{0,0}}, |
|
159 |
|
{{0,0,0}}, |
|
160 |
|
{{0,0,0,0}}, |
|
161 |
|
{{0,0,1,0,0}} |
156 |
162 |
}}; |
}}; |
157 |
163 |
|
|
158 |
164 |
Path path = bfs(graph, start, [&](Vertex v) { return (v == goal); }); |
Path path = bfs(graph, start, [&](Vertex v) { return (v == goal); }); |
File opdracht3/peg.hpp changed (mode: 100644) (index 85363fd..c8f162e) |
10 |
10 |
#include <stack> |
#include <stack> |
11 |
11 |
#include <set> |
#include <set> |
12 |
12 |
|
|
13 |
|
enum class Move { l, r, u, d }; |
|
14 |
|
using State = std::array<std::array<int,3>,3>; |
|
|
13 |
|
enum class Move { lu, ru, ld, rd }; |
|
14 |
|
using State = std::array<std::array<int,5>,5>; |
15 |
15 |
|
|
16 |
16 |
using Vertex = State; |
using Vertex = State; |
17 |
17 |
using Path = std::vector<Vertex>; |
using Path = std::vector<Vertex>; |