Using Queue

I am new in programming, and I am trying to implement Queue and back tracking. I made a program but I am lost trying to do the next description:


Use a queue to find paths through the maze (from Start to Goal) utilizing your maze program. Illustrate the progress through the maze at each step. Your program should visually distinguish between the path identified and any backtracking that may have occurred. In addition, illustrate the content of the queue at each step.


/*here is my work so far, if you can help me, it will be well appreciated;*/

#include "stdafx.h"
#include<stdio.h>



// Defining the graphic side of rickmaze
#define M 10
/* Putting a recursive function to solve maze problem*/
bool solveRickMazeUtil(int rickmaze[M][M], int x, int y, int solving[M][M]);

/* A function to print the matrix */
void printSolution(int solving[M][M])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < M; j++)
printf(" %d ", solving[i][j]);
printf("\n");
}
}

/* check if x,y is valid index for M*M maze */
bool isSafe(int rickmaze[M][M], int x, int y)
{

/*returning false if x,y outside the rickmaze*/
if(x >= 0 && x < M && y >= 0 && y < M && rickmaze[x][y] == 1)
return true;

return false;
}

/* Using backtracking with this function rickmaze will be solved. When
the path is posible should return true and when it is not false */



bool solveRickMaze(int rickmaze [M][M])
{
int solving[M][M] = {
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},
{0, 0, 0, 0, 0, 0,0,0,0},

};

if(solveRickMazeUtil(rickmaze, 0, 0, solving) == false)
{
printf("Sorry Solution is not possible");
return false;
}

printSolution(solving);
return true;
}

/* Using the recursive function to solve rickmaze problem*/


bool solveRickMazeUtil(int rickmaze[M][M], int x, int y, int solving[M][M])
{

/* if (x,y is goal) return true*/

if(x == M-1 && y == M-1)
{
solving[x][y] = 1;
return true;
}

/* This loop will check if the maze [x] [y] is valid */
if(isSafe(rickmaze, x, y) == true)
{
/*Marking x,y as the path solution */
solving[x][y] = 1;

/* Moving forward in x direction */
if (solveRickMazeUtil(rickmaze, x+1, y, solving) == true)
return true;

/* When it is moving in x direction and there is a wall then it is set to
move in y direction */

if (solveRickMazeUtil(rickmaze, x, y+1, solving) == true)
return true;

/*It will backtrack if any of the prveious movement won't work;this
will mark x,y as part of the solution road*/


solving[x][y] = 0;
return false;
}

return false;
}

/* Program to test the function above*/

int main()
{

int rickmaze [M][M] = {
{1,1,1,1,0,1,1,1,1,1},
{1,0,0,0,1,1,0,0,0,1},
{1,0,1,0,1,0,1,0,1,1},
{1,1,1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,1,1,1},
{1,1,1,1,1,0,1,1,0,1},
{0,1,1,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,1,1,0},
{1,1,0,1,1,1,0,1,0,1},
{0,1,1,1,0,1,0,1,1,1},

};

solveRickMaze(rickmaze);
return 0;
}

Topic archived. No new replies allowed.