The maze is defined as a square matrix. For example:
int maze[N][N] =
{ { 1, 1, 1, 1, 1, 1, 1 },
{ 0, 1, 0, 1, 0, 0, 1 },
{ 0, 1, 0, 1, 1, 1, 1 },
{ 0, 1, 0, 0, 0, 1, 1 },
{ 0, 1, 1, 1, 0, 1, 1 },
{ 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1 } };
You can walk only where there's a 1. You can take a step down, up, left, right. You start at the top-left corner and finish at down-right corner.
The output should be the minimum steps to finish any maze. We can assume there is at least one way to finish the maze. i've edited the code and i thought i covered everything.. but obviously i'm missing something. thanks for your help
int path_help(int maze[][N], int row, int col, int count)
{
int copy1[N][N], copy2[N][N], copy3[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
copy1[i][j] = maze[i][j];
copy2[i][j] = maze[i][j];
copy3[i][j] = maze[i][j];
}
}
int a, b, c, d;
if (col == 0 || row == 0)
{
if (row == N - 1)
{
if (maze[row][col + 1] == 1)
{
maze[row][col] = 0;
return path_help(maze, row, col + 1, count + 1);
}
else return N*N;
}
if (col == N - 1)
{
if (maze[row + 1][col] == 1)
{
maze[row][col] = 0;
return path_help(maze, row + 1, col, count + 1);
}
else return N*N;
}
if (maze[row][col + 1] == 1 && maze[row + 1][col] == 1)
{
maze[row][col] = 0;
copy1[row][col] = 0;
return min(path_help(copy1, row, col + 1, count + 1),path_help(maze, row + 1, col, count + 1));
}
if (maze[row][col + 1] == 0 && maze[row + 1][col] == 1)
{
maze[row][col] = 0;
return path_help(maze, row + 1, col, count + 1);
}
if (maze[row + 1][col] == 0 && maze[row][col + 1] == 1)
{
maze[row][col] = 0;
return path_help(maze, row, col + 1, count + 1);
}
else return N*N;
}
if (col == N - 1 || row == N - 1)
{
if (col == N - 1 && row == N - 1) return count;
if (row == N - 1)
{
if (maze[row - 1][col] == 1 && maze[row][col + 1] == 1)
{
maze[row][col] = 0;
copy1[row][col] = 0;
return min(path_help(copy1, row, col + 1, count + 1), path_help(maze, row - 1, col, count + 1));
}
if (maze[row - 1][col] == 0 && maze[row][col + 1] == 1)
{
maze[row][col] = 0;
return path_help(maze, row, col + 1, count + 1);
}
if (maze[row][col + 1] == 0 && maze[row - 1][col] == 1)
{
maze[row][col] = 0;
return path_help(maze, row - 1, col, count + 1);
}
else return N*N;
}
if (col == N - 1)
{
if (maze[row + 1][col] == 1 && maze[row][col - 1] == 1)
{
maze[row][col] = 0;
copy1[row][col] = 0;
return min(path_help(copy1, row, col - 1, count + 1), path_help(maze, row + 1, col, count + 1));
}
if (maze[row + 1][col] == 0 && maze[row][col - 1] == 1)
{
maze[row][col] = 0;
return path_help(maze, row, col - 1, count + 1);
}
if (maze[row][col - 1] == 0 && maze[row + 1][col] == 1)
{
maze[row][col] = 0;
return path_help(maze, row + 1, col, count + 1);
}
else return N*N;
}
}
if (maze[row + 1][col] == 1)
{
maze[row][col] = 0;
a = path_help(maze, row + 1, col, count + 1);
}
else a = N*N;
if (maze[row - 1][col] == 1)
{
copy1[row][col] = 0;
b = path_help(copy1, row - 1, col, count + 1);
}
else b = N*N;
if (maze[row][col + 1] == 1)
{
copy2[row][col] = 0;
c = path_help(copy2, row, col + 1, count + 1);
}
else c = N*N;
if (maze[row][col - 1] == 1)
{
copy3[row][col] = 0;
d = path_help(copy3, row, col - 1, count + 1);
}
else d = N*N;
return min(min(a, b),min( c, d));
}