Subject | Hash | Author | Date (UTC) |
---|---|---|---|
Unix-style bracketing on opdracht3. Remove carriage returns ('\r') in opdracht3. Add Bread-first search to peg.cpp. Update gitignore: Ignore all files in opdracht3 except for source files. | 17234cdf77357d8da3efa75f959e9559eb0faf91 | Jan Allersma | 2018-04-17 07:24:52 |
First attempt at completing the assignments. | acd1a9c82a11066cefa28cf97afdf6f708ecefa0 | Jan Allersma | 2018-04-06 17:40:28 |
Initial commit. All original assignments. | d1c1567a0ded18a38ba9d43e2a79ca8ad94d4ac5 | Jan Allersma | 2018-04-06 17:34:42 |
File | Lines added | Lines deleted |
---|---|---|
.gitignore | 4 | 0 |
opdracht3/peg.cpp | 35 | 9 |
opdracht3/peg.hpp | 0 | 0 |
File .gitignore added (mode: 100644) (index 0000000..e2c4c87) | |||
1 | opdracht3 | ||
2 | !*.cpp | ||
3 | !*.hpp | ||
4 | !*.h |
File opdracht3/peg.cpp renamed from opdracht3/8puzzle.cpp (similarity 77%) (mode: 100644) (index 1cc21cb..d4683b0) | |||
1 | 1 | // 8puzzle.cpp | // 8puzzle.cpp |
2 | 2 | // Compile with: g++ 8puzzle.cpp -o 8puzzle | // Compile with: g++ 8puzzle.cpp -o 8puzzle |
3 | 3 | ||
4 | #include "8puzzle.h" | ||
4 | #include "peg.hpp" | ||
5 | #include <queue> | ||
5 | 6 | ||
6 | 7 | typename std::vector<Vertex>::const_iterator Graph::cbegin(Vertex v) const | typename std::vector<Vertex>::const_iterator Graph::cbegin(Vertex v) const |
7 | 8 | { | { |
... | ... | Path dfs(const Graph &graph, const Vertex &start, std::function<bool(const Verte | |
65 | 66 | std::stack<Path> queue; | std::stack<Path> queue; |
66 | 67 | std::set<Vertex> visited; | std::set<Vertex> visited; |
67 | 68 | Path path; | Path path; |
68 | |||
69 | |||
69 | 70 | queue.push(path); | queue.push(path); |
70 | 71 | while (!queue.empty()) { | while (!queue.empty()) { |
71 | 72 | path = queue.top(); | path = queue.top(); |
72 | 73 | queue.pop(); | queue.pop(); |
73 | |||
74 | |||
74 | 75 | Vertex last; | Vertex last; |
75 | 76 | if (path.size() == 0) { | if (path.size() == 0) { |
76 | 77 | last = start; | last = start; |
77 | 78 | } else { | } else { |
78 | 79 | last = path.back(); | last = path.back(); |
79 | 80 | } | } |
80 | if (goalTest(last)) | ||
81 | if (goalTest(last)) | ||
81 | 82 | return path; // path is a vector of Vertices | return path; // path is a vector of Vertices |
82 | |||
83 | |||
83 | 84 | if (visited.find(last) == visited.end()) { | if (visited.find(last) == visited.end()) { |
84 | 85 | visited.insert(last); | visited.insert(last); |
85 | 86 | for (auto it = graph.cbegin(last); it != graph.cend(); it++) { // extend path with new Vertex | for (auto it = graph.cbegin(last); it != graph.cend(); it++) { // extend path with new Vertex |
... | ... | Path dfs(const Graph &graph, const Vertex &start, std::function<bool(const Verte | |
92 | 93 | return Path(); | return Path(); |
93 | 94 | } | } |
94 | 95 | ||
95 | Path bfs(const Graph &graph, const Vertex &start, std::function<bool(const Vertex &vertex)> goalTest) | ||
96 | { | ||
97 | // Here goes the solution to exercise #2 | ||
96 | Path bfs(const Graph &graph, const Vertex &start, std::function<bool(const Vertex &vertex)> goalTest) { | ||
97 | std::queue<Path> queue; | ||
98 | std::set<Vertex> visited; | ||
99 | Path path; | ||
100 | |||
101 | queue.push(path); | ||
102 | while (!queue.empty()) { | ||
103 | path = queue.front(); | ||
104 | queue.pop(); | ||
105 | |||
106 | Vertex last; | ||
107 | if (path.size() == 0) { | ||
108 | last = start; | ||
109 | } else { | ||
110 | last = path.back(); | ||
111 | } | ||
112 | if (goalTest(last)) | ||
113 | return path; // path is a vector of Vertices | ||
114 | |||
115 | for (auto it = graph.cbegin(last); it != graph.cend(); it++) { // extend path with new Vertex | ||
116 | if(visited.find(*it) == visited.end()) { | ||
117 | Path n = path; | ||
118 | n.push_back(*it); | ||
119 | queue.push(n); | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | return Path(); // return empty path | ||
98 | 124 | } | } |
99 | 125 | ||
100 | int main() | ||
126 | int main() | ||
101 | 127 | { | { |
102 | 128 | Graph graph; | Graph graph; |
103 | 129 |
File opdracht3/peg.hpp renamed from opdracht3/8puzzle.h (similarity 100%) |