![]() |
| ||
| Re: Need help in finding correct method for my program! Ah yes. I'm sorry for leaving out these information. Basically, this is a "game" of the mouse and cheese. The mouse is represented with M, which is the starting point of the maze itself. C is the cheese, which is the ending point of the maze. Thus, the objective of the whole program is to reach from M to C! The highlighted text from main function: for (int start_loop = 1; start_loop>0; start_loop++) The red highlighted text is the "infinite loop", which will keep prompting the user for an action after a particular action has been performed. I'm really sorry if I've confused you... The for loop in the main function is just to show the maze, it has no relation in the pathfinding/insert the maze into array. Once again, I'm sorry if I didn't provide sufficient information. Here's the results I've gotten so far... http://i37.tinypic.com/2rpblzm.jpg |
| ||
| Re: Need help in finding correct method for my program! For debugging purposes, you need to display each iteration of your maze solving function to see what's happening. Take your display code and make a function out of it. void Display () At the top of the findPath () function, add these two lines: Display (); cin.get ()pauses. Press the enter key to see the next iteration. It will give you a much better feel for what is going on step by step. These two lines are debugging code, so you need to remember to eventually take them out. |
| ||
| Re: Need help in finding correct method for my program! Backtracking can be made more efficient if you can keep track of where you've been. As I said in my earlier post a stack is commonly used but you could use an array or list almost as readily. Since you are already using an array for the maze I assume you are reasonably familiar with their use. So, I'd declare a one dimensional array of type struct called path or solution or whatever with the same number of total objects as in the maze. I'd als declare an int to keep track of how many cells of the maze you actually have in the path. As you go to each new cell of the maze add it to the end of the array. When you can't go any further with a given cell then you can effectively remove that cell from the path by decreasing the value of the variable keeping track of the number of cells actually in the path at the same time you place the '+' in the cell of the maze that is the dead end. Then it's a simple task print out the path when you find a solution to the maze. You will probably need to do bounds check sooner or later, too. That means if the Mouse is in the top row it can't go up (South?) any further, if it's in the far right row it go right (East) any further, etc. Next moves can be checked in same order every time----North, East, South, West, etc. Valid next moves for the Mouse would be elements of the maze that have values C or space. First valid move found in that sequence put 0 in current cell, move m (the actual mouse, which is separate from M which is the starting position of the Mouse) in the next valid move cell and repeat. If the move puts the Mouse out of bounds and if the next element of the maze is *, +, 0, or M then that move is invalid. If no valid move is found after looking in all four directions, then put a + in the current cell and decrease the length of the current path by one and put m in that cell and retry. If the length of the path ever gets to minus one (or zero or other value meaning it is empty) then there is no solution. The process stops when there is a solution or the path is empty. If there is a solution then print it out. |
| All times are GMT -4. The time now is 4:05 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC