Question:
I have to write a simple Path Traversal Algorithm for a maze given to me using recursion.?
Jayu P
2007-03-01 14:59:58 UTC
I suppos to write a recursive program that walks trough a maze. The maze will be defined as a two-dimensional array of characters (hashes ‘#’ and dots ‘.’). The hashes represent the walls in the maze and the dots represent empty (and non-visited) squares in the possible paths through the maze. Here is what I have so far.

#include
using namespace std;

int col ;
int row ;
const int rows = 12;
const int column = 12;

char maze[rows][column] =
{
{ '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' },
{ '#' , '.' , '.' , '.' , '#' , '.' , '.' , '.' , '.' , '.' , '.' , '#' },
{ '.' , '.' , '#' , '.' , '#' , '.' , '#' , '#' , '#' , '#' , '.' , '#' },
{ '#' , '#' , '#' , '.' , '#' , '.' , '.' , '.' , '.' , '#' , '.' , '#' },
{ '#' , '.' , '.' , '.' , '.' , '#' , '#' , '#' , '.' , '#' , '.' , '.' },
{ '#' , '#' , '#' , '#' , '.' , '#' , '.' , '#' , '.' , '#' , '.' , '#' },
{ '#' , '.' , '.' , '#' , '.' , '#' , '.' , '#' , '.' , '#' , '.' , '#' },
{ '#' , '#' , '.' , '#' , '.' , '#' , '.' , '#' , '.' , '#' , '.' , '#' },
{ '#' , '.' , '.' , '.' , '.' , '.' , '.' , '.' , '.' , '#' , '.' , '#' },
{ '#' , '#' , '#' , '#' , '#' , '#' , '.' , '#' , '#' , '#' , '.' , '#' },
{ '#' , '.' , '.' , '.' , '.' , '.' , '.' , '#' , '.' , '.' , '.' , '#' },
{ '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' }
};
void printMaze();
void runMaze(int, int);

void printMaze()
{
for(int row = 0; row < rows; row++)
{
for(int col=0; col < column; col++)
cout << maze[row][col];
cout << "\n";
}
}

//void runMaze(int row, int col)
//{

void runMaze (int row, int col)
{
for
( (row >= 0 && row < rows) && (col >= 0 && col < column))
{
for
( maze[row][col] == 'e' )

return;

for
( maze[row][col] == '.')
{
maze[row][col]='X';

switch (disp)
{
case 1:
col+1;
break;
case 2:
col-1;
break;
case 3:
row-1;
break;
case 4
row+1;
break;
default:
cout << "you are free";
}
}
}
int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
runMaze(2, 0);
printMaze();
return 0;
}
}
Three answers:
its_ramzi
2007-03-01 15:12:49 UTC
I'm not interesting in discussing the program implementation, but I should point you in the correct direction for solving a maze if you do not know how.



The maze appears to be simply connected. This means it can be solved by hugging one wall. Good luck.
?
2016-11-27 03:55:17 UTC
the main necessary strategies that the folk can attempt against international warming is getting rid of their older type vehicles (1979 Buick) because of fact, older vehicles emmitt greater poisonous fumes into the air. Its glaring that the greater technological better a vehicle is, the fewer risky gases it emmitts. A vehicle that grow to be geared up 3 or 4 years in the past would be greater financial gadget friendly than a vehicle that grow to be assembled 35 years in the past. for top populated cities collectively with huge apple, Bus and Taxi companys could replace contemporary automobiles with vehicles that are powered by hydrogen or electricity. in basic terms think of, if each and every significant city used the Toyota Prius as taxi's, there may well be a decreased point of pollutants, and likewise taxi fares must be severely decrease besides.
2007-03-01 15:16:56 UTC
Don't try to solve a maze by hugging a wall, that is a shitty way to solve them.



Do it recursively.



Use the rule: Go forward, go left, go right. If none of that works, then turn around.



Now, simply call itself constantly and you will come up with a solution.



It's rather easy.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...