I've done quite some java coding already, but I'm totally new to c++ and have no idea what's going on with my code right now. This code gives me a compile error in the map standard library. It says: Cannot increment value of type ' std::_1::pari<int, int>'
, and is happening in map
, insert(_InputIterator __f, _InputIterator __l)
if that's of any relevance.
I know the stackoverflow community generally doesn't like solving other peoples homework, but I think I made a genuine attempt at implementing this and besides that I'm very curious as to what's going on.
typedef std::pair<int, int> location;
std::set<location> neighbours(location loc, std::set<std::pair<location, location>> labyrinth, int& size) {
std::set<location> neighbours;
location locFmin = location(loc.first - 1, loc.second);
location locSmin = location(loc.first, loc.second - 1);
location locFplus = location(loc.first + 1, loc.second);
location locSplus = location(loc.first, loc.second + 1);
if (loc.first - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locFmin)) == labyrinth.end()) {
neighbours.insert(locFmin);
}
if (loc.second - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locSmin)) == labyrinth.end()) {
neighbours.insert(locSmin);
}
if (loc.first + 1 < size && labyrinth.find(std::pair<location, location>(loc, locFplus)) == labyrinth.end()) {
neighbours.insert(locFplus);
}
if (loc.second + 1 < size && labyrinth.find(std::pair<location, location>(loc, locSplus)) == labyrinth.end()) {
neighbours.insert(locSplus);
}
return neighbours;
}
int Labyrinth(std::set<std::pair<location, location>> labyrinth, int size) {
std::map<location, location> forest;
std::set<location> level;
std::set<location> known;
known.insert(location(0,0));
level.insert(location(0,0));
while (!level.empty()) {
std::set<location> nextLevel;
for (location loc: level) {
for (location neighbour: neighbours(loc, labyrinth, size)) {
if (known.find(neighbour) != known.end()) {
known.insert(neighbour);
forest.insert(neighbour, loc);
nextLevel.insert(neighbour);
}
}
}
level = nextLevel;
}
std::list<location> path;
location walk = location(size - 1, size - 1);
path.push_front(walk);
while (walk != location(0, 0)) {
walk = forest[walk];
path.push_front(walk);
}
int answ = path.size();
return answ;
}
It's an algorithm that should perform a breadth-first search trough a square maze of size size
with of course size * size location(x, y)
objects.
the incoming list labyrinth
defines the walls of the maze one can't go trough. Eventually the function should return the number of nodes contained in the shortest path from (0, 0) to (size - 1, size - 1).
this is a simple test for the algorithm
std::set<std::pair<location, location> > labyrinth;
labyrinth.insert(std::pair<location, location>(location(0, 0), location(1, 0)));
labyrinth.insert(std::pair<location, location>(location(0, 1), location(1, 1)));
labyrinth.insert(std::pair<location, location>(location(0, 2), location(0, 3)));
labyrinth.insert(std::pair<location, location>(location(1, 1), location(1, 2)));
labyrinth.insert(std::pair<location, location>(location(1, 2), location(2, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 3), location(3, 3)));
labyrinth.insert(std::pair<location, location>(location(2, 2), location(3, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 1), location(3, 1)));
int labAnswer = Labyrinth(labyrinth, 4);
std::cout << labAnswer << std::endl;
if (labAnswer == 13)
{
std::cout << "Correct" << std::endl;
}
else
{
std::cout << "Incorrect" << std::endl;
}
Before anyone starts to come up with better ideas to solve this problem. I got the idea for the bfs code from a graph bfs java implementation from a book on algorithms. I'm not interested in solving this puzzle more efficiently, there'll always be better ways to do something. I'd like to know what is going on with my code and possibly what c++ aspect I'm missing here.
You aren't using std::map correctly with insert,
the line
forest.insert(neighbour, loc);
should be
forest[neighbor] = loc;