I am trying to read a maze (from input.txt
) and solve it. This code doesn't work as it should. What is wrong?
void Maze::create(vector<vector<int> >&info)
{
for (int i = 1; (unsigned)i < info.size(); i++)
{
for (int j = 0; (unsigned)j < info[i].size(); j++)
{
if (info[i][j] == wall)
{
map[i - 1][j] = block;
}
else if (info[i][j] == path)
{
map[i - 1][j] == ' ';
}
else if (info[i][j] == start)
{
map[i - 1][j] = 'S';
startingX = (i - 1);
startingY = j;
}
else if (info[i][j] == end)
{
map[i - 1][j] = 'E';
endingX = (i - 1);
endingY = j;
}
else if (info[i][j]>=bonus)
{
map[i - 1][j] = 'B';
}
}
}
print();
cout << endl;
if (solve(startingX, startingY))
{
print();
}
else
{
cout << "DAMN" << endl;
}
}
The above is the function in which I read the input file called "info" and translate to the Maze called "map".
bool Maze::solve(int a, int b)
{
map[b][a] = road;
cout << "yes"<<endl;
if (a == endingX&&b == endingY)
{
cout << "yes1" << endl;
return true;
}
else if (a > 0 && map[b][a - 1] ==free && solve((a - 1), b))
{
cout << "yes2" << endl;
return true;
}
else if (a < map[0].size() && map[b][a + 1] == free&& solve((a + 1), b))
{
cout << "yes3" << endl;
return true;
}
else if (b > 0 && map[b - 1][a] == free && solve(a, (b - 1)))
{
cout << "yes4" << endl;
return true;
}
else if (b < map.size() && map[b + 1][a] == free && solve(a, (b + 1)))
{
cout << "yes5" << endl;
return true;
}
map[b][a] == free;
//print();
return false;
}
and that is my solving code. I try to use the recursive method but seems like it doesn't work but just showing me the starting point only.