I'm trying to determine if the characters in the maze can escape, the characters are labeled by their initials except for D which is the door here is the input data:
11 13
7 10
XXXXXXXXXXXXX
X JX AX X
XXXX XXXXBX X
X X SX X X
X XXXX XXX X
X X X X
X XXXXXXXXX X
X X X
XXXXXX XXXX X
XL X
XXXXXXXDXXXXX
this is the input but the spacing is off some of the X's should be up against the right hand side the top two numbers are the size of the maze and the next two are the location of the door. I'm having a problem with my runMaze function, because it keeps returning true when really the character should not be able to escape such as in J's case where he is surrounded by X's. It keeps returning true and I have a feeling it's not checking against the X's correctly or leaving * in the worng spot and going back to the same spot????

#include <stdio.h>

void load(char x[][1000], int collum, int row, char ln );
bool runMaze(char x[][1000],int row, int collum, const int MAXR, const int MAXC);

int main()
{
   int row = 0;
   int collum = 0;
   int doorx;
   int doory;
   char ln;
   int jcordx, Acordx, Bcordx, Scordx, Alcordx;
   int jcordy, Acordy, Bcordy, Scordy, Alcordy;
   bool escapeJ = false;
   bool escapeA = false;

   scanf("%d", &row);
   scanf("%d", &collum);
   scanf("%c",ln);
   scanf("%d", &doorx);
   scanf("%d", &doory);
   scanf("%c", &ln);

   const int MAXR = row;
   const int MAXC = collum;

   char maze[collum][1000];
   load(maze, collum, row, ln);

   for (int i = 0; i < row + 1; i++ )
   {
      for(int j = 0; j < collum; j++ )
      {
         if(maze[i][j] == 'J')
         {
            jcordx = j;
            jcordy = i;
            escapeJ = runMaze(maze, collum, row, MAXR, MAXC);
         }
         if(maze[i][j] == 'A')
         {
            Acordx = j;
            Acordy = i;
            escapeA = runMaze(maze, collum, row, MAXR, MAXC);
 }
         if(maze[i][j] == 'A')
         {
            Acordx = j;
            Acordy = i;
            escapeA = runMaze(maze, collum, row, MAXR, MAXC);
         }
         if(maze[i][j] == 'S')
         {
            Scordx = j;
            Scordy = i;
         }
         if(maze[i][j] == 'L')
         {
            Alcordx = j;
            Alcordy = i;
         }
         if(maze[i][j] == 'B')
         {
            Bcordx = j;
            Bcordy = i;
         }
      }
   }
   if(escapeJ == true)
      printf("Jamie - Position(%d,%d): Escape\n", jcordx,jcordy);
   else
      printf("Jamie - Position(%d,%d): No Escape\n", jcordx,jcordy);
   if(escapeA == true)
      printf("Adam - Position(%d,%d): Escape\n", Acordx,Acordy);
   else
      printf("Adam - Position(%d,%d): No Escape\n", Acordx,Acordy);

   printf("Sam - Position(%d,%d):\n", Scordx,Scordy);
   printf("Al - Position(%d,%d):\n", Alcordx,Alcordy);
   printf("Buster - Position(%d,%d):\n", Bcordx,Bcordy);
}
bool runMaze(char x[][1000],int row, int collum, const int MAXR, const int MAXC)
{
   x[collum][row];
   if( (row>0 && row<MAXR) && (collum>0 && collum< MAXC))
   {
      if( x[row][collum] == 'D' ) return true;
      {
         if( x[row][collum] == ' ')
 if( x[row][collum] == ' ')
         {
            x[row][collum]='*';
            runMaze(x,row, collum+1, MAXR, MAXC);
            runMaze(x,row, collum-1, MAXR, MAXC);
            runMaze(x,row-1, collum, MAXR, MAXC);
            runMaze(x,row+1, collum, MAXR, MAXC);
         }
      }
   }

}

void load(char x[][1000], int collum, int row, char ln)
{
   x[collum][row];
   for(int i = 0; i < row + 1; i++ )
   {
      for( int j = 0; j < collum; j++)
      {
         scanf("%c",&x[i][j]);
      }
      scanf("%c",&ln);
   }
}

Note: im only checking the first two characters.

this is C. "bool" types are not allowed. neither are you allowed to declare variables within a for loop's conditional statement. the fact that you are able to get away with it using a C++ compiler doesn't matter. standard C compilers this fails to compile\

that said, ive gone ahead and changed bool to int, ive replaced true and false with 1 and 0, and ive properly declared your index variables i and j outside the for conditional.

now the program compiles but doesnt do anything, just sits there blankly.

what was your question anyhow?

if you want help troubleshooting you need to post compilable code with a definite question about some behavior that can be reproduced. if it's not apparent on how to reproduce the problem, then you need to explain to us how to get where you are.

as in this case: i have no idea how this program should work. maybe i could spend my time trying to figure it out ... but i'm not going to.

I'm trying to write a program to traverse the cells of the prison to determine if an individual can escape. There is one way in and out labeled 'D' for door. Individuals can only move north, south, east, and west. The teleportation location of an individual is given by their first initial, J, A, B, iand S except for AL which is L. Walls are given by an X. Let the top left corner of the map be location 0,0. Output the X,Y location of each person and indicate if the can escape.

Sample Input: The first line is the size of the MAP, number of rows, n followed by the number of columns, m. Maximum value of n and m is 1000. The second line of input is the (x,y) location of the door assuming the top left corner is position (0,0). The remaining n lines of input represent the map. Each row is m characters long. the input above is what goes into the arrays but it is not exact because the spacing got compromised when I pasted it

My question is why is my recursive function always returning 1 when it shouldn't?

Member Avatar for iamthwee
#include <stdio.h>
#include <string.h>

#define FALSE 0
#define TRUE 1

#define NROWS 7
#define MCOLS 7

// Symbols:
// '.' = open
// '#' = blocked
// 'S' = start
// 'G' = goal
// '+' = path
// 'x' = bad path
char maze[NROWS][MCOLS] = {
    "S...##",
    "#.#...",
    "#.##.#",
    "..#.##",
    "#...#G",
    "#.#..."
};


void display_maze(void);
int find_path(int x, int y);


int main(void)
{
    display_maze();

    if ( find_path(0, 0) == TRUE )
        printf("Success!\n");
    else
        printf("Failed\n");

    display_maze();
    getchar();

    return 0;
}
// main()


void display_maze(void)
{
    int i;

    printf("MAZE:\n");
    for ( i = 0; i < NROWS; i++ )
        printf("%.*s\n", MCOLS, maze[i]);
    printf("\n");

    return;
}


int find_path(int x, int y)
{
    // If x,y is outside maze, return false.
    if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;

    // If x,y is the goal, return true.
    if ( maze[y][x] == 'G' ) return TRUE;

    // If x,y is not open, return false.
    if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;

    // Mark x,y part of solution path.
    maze[y][x] = '+';

    // If find_path North of x,y is true, return true.
    if ( find_path(x, y - 1) == TRUE ) return TRUE;

    // If find_path East of x,y is true, return true.
    if ( find_path(x + 1, y) == TRUE ) return TRUE;

    // If find_path South of x,y is true, return true.
    if ( find_path(x, y + 1) == TRUE ) return TRUE;

    // If find_path West of x,y is true, return true.
    if ( find_path(x - 1, y) == TRUE ) return TRUE;

    // Unmark x,y as part of solution path.
    maze[y][x] = 'x';

    return FALSE;
}
// find_path()

Maybe... taken from
http://www.dreamincode.net/forums/showtopic94248.htm

So not tested!

#include <stdio.h>

void load(char x[][1000], int collum, int row, char ln );
//int runMaze(char x[][1000],int row, int collum, const int MAXR, const int MAXC );
int find_path(char maze[][1000],int x, int y, const int MCOLS, const int MROWS);
int main()
{
   int row = 0;
   int collum = 0;
   int doorx;
   int doory;
   char ln;
   int jcordx, Acordx, Bcordx, Scordx, Alcordx;
   int jcordy, Acordy, Bcordy, Scordy, Alcordy;
   int escapeJ = 0;
   int escapeA = 0;
   int escapeS = 0;
   int escapeL = 0;
   int escapeB = 0;

   scanf("%d", &row);
   scanf("%d", &collum);
   scanf("%c",ln);
   scanf("%d", &doorx);
   scanf("%d", &doory);
   scanf("%c", &ln);

   const int MAXR = row;
   const int MAXC = collum;

   char maze[collum][1000];
   load(maze, collum, row, ln);

   int i, j;

   for ( i = 0; i < row + 1; i++ )
   {
      for( j = 0; j < collum; j++ )
      {
         if(maze[i][j] == 'J')
         {
            jcordx = j;
            jcordy = i;
            //escapeJ = find_path(maze, jcordy,jcordx, MAXR, MAXC);
         }
         if(maze[i][j] == 'A')
         {
            Acordx = j;
            Acordy = i;
            //escapeA = find_path(maze,Acordy, Acordx, MAXC, MAXR);
         }
         if(maze[i][j] == 'S')
         {
            Scordx = j;
            Scordy = i;
            escapeS = find_path(maze,Scordx, Scordy, MAXC, MAXR);
         }
         if(maze[i][j] == 'L')
         {
            Alcordx = j;
            Alcordy = i;
            //escapeL = find_path(maze,Alcordx, Alcordy, MAXC, MAXR);
         }
         if(maze[i][j] == 'B')
         {
            Bcordx = j;
            Bcordy = i;
            //escapeB = find_path(maze,Bcordx, Bcordy, MAXC, MAXR);
         }
      }
   }
   if(escapeJ == 1)
      printf("Jamie - Position(%d,%d): Escape\n", jcordx,jcordy);
   else
      printf("Jamie - Position(%d,%d): No Escape\n", jcordx,jcordy);
   if(escapeA == 1)
      printf("Adam - Position(%d,%d): Escape\n", Acordx,Acordy);
   else
      printf("Adam - Position(%d,%d): No Escape\n", Acordx,Acordy);
   if(escapeS == 1)
      printf("Sam - Position(%d,%d): Escape\n", Scordx,Scordy);
   else   
      printf("Sam - Position(%d,%d): No Escape\n", Scordx,Scordy);
   if(escapeL == 1)
      printf("Al - Position(%d,%d): Escape\n", Alcordx,Alcordy);
   else
      printf("Al - Position(%d,%d): No Escape\n", Alcordx,Alcordy);
   if(escapeB == 1)
      printf("Buster - Position(%d,%d): Escape\n", Bcordx,Bcordy);
   else   
      printf("Buster - Position(%d,%d): No Escape\n", Bcordx,Bcordy);
}
int find_path(char maze[][1000], int x, int y,const int MCOLS, const int NROWS)
{
   maze[MCOLS][NROWS];
   // If x,y is outside maze, return false.
   if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return 0;

   // If x,y is the goal, return true.
   if ( maze[y][x] == 'D' ) return 1;

   // If x,y is not open, return false.
   if ( maze[y][x] != ' '&&  maze[y][x] != 'S' ) return 0;

   // Mark x,y part of solution path.
   maze[y][x] = '+';

   // If find_path North of x,y is true, return true.
   if ( find_path(maze,x, y - 1,MCOLS, NROWS) == 1 ) return 1;

   // If find_path East of x,y is true, return true.
   if ( find_path(maze,x + 1, y, MCOLS,  NROWS) == 1 ) return 1;

   // If find_path South of x,y is true, return true.
   if ( find_path(maze,x, y + 1, MCOLS,  NROWS) == 1 ) return 1;

   // If find_path West of x,y is true, return true.
   if ( find_path(maze,x - 1, y, MCOLS,  NROWS) == 1 ) return 1;

   // Unmark x,y as part of solution path.
   //maze[y][x] = 'x';

   return 1;
}

void load(char x[][1000], int collum, int row, char ln)
{
   int i =0;
   int j =0;

   x[collum][row];
   for( i = 0; i < row + 1; i++ )
   {
      for( j = 0; j < collum; j++)
      {
         scanf("%c",&x[i][j]);
      }
      scanf("%c",&ln);
   }
}

//int search(char x[][1000], int col)

/* for ( int i = 0; i < row + 1; i++ )
   {
   for ( int j = 0; j < collum; j++)
   {
   printf("%c", x[i][j]);
   }
   }
   */

I tried this and this is what I got,

Jamie - Position(2,1): No Escape
Adam - Position(7,1): No Escape
Sam - Position(7,3): Escape
Al - Position(1,9): No Escape
Buster - Position(9,2): No Escape

Sam should not be able to escape, and I can't call the function multiple times bc in destroys my maze after one go I'm was trying to think of how to do it without compromising my maze.

sorry for the spacing issue

I can't call the function multiple times bc in destroys my maze after one go I'm was trying to think of how to do it without compromising my maze.

all you have to do is store the original maze in memory and recall it each time with the new person's position.

you've been handed 90% of the solution and 99% of the difficulty. all you need to do now is construct a for or while loop to implement it several times.

commented: *yup* +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.