console application - How to check for the previous path searched on a maze C# -
i trying code algorithm solves maze problem facing difficulty apply correctly.
the algorithm runs on walls instead of changing direction after finding valid point.
i not understand how check previouspoint , point check next valid move.
could me giving me tips on direction go?
class mappathfinder { public bool[,] correctpath = new bool[12,12]; public int[,] previouspoint = new int[12, 12]; public bool startpointfound = false; public bool nextvalidmove(mapfile map, int y, int x) { if ((y == map.width) && (x == map.height)) { return false; //checks if @ edge , terminates method } if ((map.matrix[y, x]) == 1 ) { return true; // check if @ wall , terminate method } if (y != 0) { if (nextvalidmove(map, y-1,x)) { map.matrix[y, x] = 9; //changes color of position correctpath[y, x] = true; return correctpath[y, x]; } if (y != map.width - 1) //check if @ limit of map { if (nextvalidmove(map,y + 1, x)) { map.matrix[y, x] = 9; correctpath[y, x] = true; return correctpath[y, x]; } } if (x != 0) { if (nextvalidmove(map, y, x - 1)) { map.matrix[y, x] = 9; correctpath[y, x] = true; return correctpath[y, x]; } } if (x != map.height - 1) { if (nextvalidmove(map, y, x + 1)) { map.matrix[y, x] = 9; correctpath[y, x] = true; return correctpath[y, x]; } } } return false; } public bool pathfinder(mapfile map) { (int y = 1; y < map.width; y++) { (int x = 1; x < map.height; x++) { var status = mapdisplay.displaymap(map); if (status) { nextvalidmove(map, x, y); } } } return true; }
i tried implement answer given paul not anywhere , lost.
that got answer:
public bool nextvalidmove(mapfile map, int y, int x) { if ((y == map.width) || (x == map.height)) return false; if(y<0 || x<0) return false; if ((map.matrix[y, x]) == 1) return true; // check if @ wall , terminate method if (map.matrix[y, x] == 5) return map.end; if (y - 1 >= 0 && map.matrix[y-1, x] == 2 && !nextvalidmove(map, y-1, x)) { map.matrix[y, x] = 9; previouspoint[y, x] = map.matrix[y, x]; return false; } // test east wall... if (x + 1 <= map.width - 1 && map.matrix[y + 1, x] == 2 && !nextvalidmove(map, y, x+1)) { map.matrix[y, x] = 9; previouspoint[y, x] = map.matrix[y, x]; return false; } // test south wall... if (y + 1 <= map.height - 1 && map.matrix[y, x + 1] == 2 && !nextvalidmove(map, y+1,x)) { map.matrix[y, x] = 9; previouspoint[y, x] = map.matrix[y, x]; return false; } // test west wall... if (x - 1 >= 0 && map.matrix[y, x - 1] == 2 && !nextvalidmove(map, y, x-1)) { map.matrix[y, x] = 9; previouspoint[y, x] = map.matrix[y, x]; return false; } return false; }
when run stack overflow error.
when checking possible points , calling function recursively
!nextvalidmove(map, y-1, x)
i don't understand why checking nextvalidmove(y-1,x) since true @ begining of if statement:
if(map.matrix[y-1, x] == 2 && !nextvalidmove(y-1,x))
i thought of checking previouspoint together, this:
if(nextvalidmove(map, y - 1, x)&&!previouspoint[y-1,x])
but getting stackoverflow error. have no clue how out of there anymore.
i have rewriten mappathfinder class make work.
class mappathfinder { public const byte wall = 1; public const byte road = 2; public const byte start = 3; public const byte finish = 5; public const byte already_there = 9; public bool nextvalidmove(mapfile map, int x, int y) { // check edges if (x < 0 || x > map.width || y < 0 || y > map.height) return false; byte currentposition = map.matrix[x, y]; // check walls or there if (currentposition == wall || currentposition == already_there) return false; // print var status = mapdisplay.displaymap(map); if (status) { // check finish if (currentposition == finish) { return true; // we've arrived! } // road // // set there map.matrix[x, y] = already_there; // left if (nextvalidmove(map, x - 1, y)) return true; // right if (nextvalidmove(map, x + 1, y)) return true; // if (nextvalidmove(map, x, y - 1)) return true; // down if (nextvalidmove(map, x, y + 1)) return true; // not correct path.. map.matrix[x, y] = road; } return false; } public bool pathfinder(mapfile map) { // looking start point (int x = 0; x < map.width; x++) { (int y = 0; y < map.width; y++) { if (map.matrix[x, y] == start) return nextvalidmove(map, x, y); } } return false; } }
however left work you:
- no storing of correct path.
- if there 2 correct paths, algorithm won't take shorter one, first found.
wiki
Comments
Post a Comment