recursion - I need to write a program in C that will recursively solve a maze of X's and blank spaces -
the program must start @ index [0][1] , completes maze when gets index [7][7]. program crashes when reaches "findpath" function. can't think of i'm doing wrong, info helps.
#include "header.h" int main(void) { int x = 0, y = 1; char maze[8][8] = { {' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x', ' ', ' ', ' ', ' '}, {'x', 'x', 'x', 'x', 'x', 'x', 'x', ' '}, {'x', 'x', 'x', 'x', 'x', 'x', 'x', ' '}, }; (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { printf("%c ",maze[i][j]); } printf("\n"); } findpath(maze, x, y); return 0; } #include "header.h" void findpath(char maze[8][8], int x, int y) { if (x == 7 & y == 7) { printf("maze complete"); (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { printf("%c ", maze[i][j]); } printf("\n"); } return; } else { if (maze[x + 1][y] = ' ') { maze[x][y] = 'h'; findpath(maze, x + 1, y); return; } else if (maze[x - 1][y] = ' ') { maze[x][y] = 'h'; findpath(maze, x - 1, y); return; } else if (maze[x][y + 1] = ' ') { maze[x][y] = 'h'; findpath(maze, x, y + 1); return; } else if (maze[x][y - 1] = ' ') { maze[x][y] = 'h'; findpath(maze, x, y - 1); return; } else { printf("no path found"); return; } } }
i believe problem simpler you're making it.
primary issue approach: findpath()
needs work on trial , error basis -- don't have strategy allowing succeed and/or fail , signal such it's caller, nor undo it's wrong assumptions.
issues code: folks have mentioned =
vs. ==
use &
when mean &&
; code indentation, or code posting, needs work; should strive avoid numbers in code can swap out maze later; lower level routines should return results, not print -- higher level routines should results , decide print; lots of redundant code -- move duplicated statements level; others have mentioned, need limit checking expressions maze[x - 1][y]
can encroach on memory didn't allocate if x
zero.
below rework of codes along lines above , style changes. solves 1 maze provided:
#include <stdio.h> #include <stdbool.h> #define width (8) #define height (8) void printmaze(char maze[width][height]) { (int = 0; < width; i++) { (int j = 0; j < height; j++) { printf("%c ", maze[i][j]); } putchar('\n'); } } bool findpath(char maze[width][height], int x, int y) { maze[x][y] = 'h'; if (x == width - 1 && y == height - 1) { return true; } if (x + 1 < width && maze[x + 1][y] == ' ') { if (findpath(maze, x + 1, y)) { return true; } } if (x - 1 >= 0 && maze[x - 1][y] == ' ') { if (findpath(maze, x - 1, y)) { return true; } } if (y + 1 < height && maze[x][y + 1] == ' ') { if (findpath(maze, x, y + 1)) { return true; } } if (y - 1 >= 0 && maze[x][y - 1] == ' ') { if (findpath(maze, x, y - 1)) { return true; } } maze[x][y] = ' '; return false; } int main(void) { char maze[width][height] = { {' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}, {' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x', ' ', ' ', ' ', ' '}, {'x', 'x', 'x', 'x', 'x', 'x', 'x', ' '}, {'x', 'x', 'x', 'x', 'x', 'x', 'x', ' '}, }; printmaze(maze); if (findpath(maze, 0, 1)) { printf("maze completed!\n"); printmaze(maze); } else { printf("no path found!"); } return 0; }
output
> ./a.out x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x maze completed! h h x x x x x x h x x x x x x x h x x x x x x x h h h h h x x x x x x x h x x x x x x x h h h h x x x x x x x h x x x x x x x h >
Comments
Post a Comment