Stacks, Queues, and a Maze??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Stacks, Queues, and a Maze??

 
0
  #1
Feb 20th, 2008
We were given a task, to familiarize ourselves with stacks and queues, to write a program to solve a maze problem. We are given text files with pre-defined values letting us know the size of the maze, the mouse's position, the cheese's position as well as the value each cell carries.

I attached some extra info for a more clear understanding of this assignment.

What am trying to do....where i got some questions is.....ok so the input file is read and each cell contains an interger value that gets translated into its binary notation where starting from the left, each bit represents N E S W (North, east, south and west). How do i have it recognize where there are 1's and 0's ( 1 signifies a pathway within the room, 0...a c omplete wall) within the binary number. She provided us with the below header file....my questions is the "&" symbol does what? doorEncoding(which is the value of the cell) & 0x08...what does that mean? am thinking it compares the number's 1 positions after the symbol to the cell value?

Maze.h
  1. typedef struct
  2. {
  3. unsigned short int doorEncoding; // Range 0..15.
  4. north = doorEncoding & 0x08 // 8 = 1000 ( north position has 1)
  5. east = doorEncoding & 0x04 // 4 = 0100 ( east position has 1)
  6. south = doorEncoding & 0x02
  7. west = doorEncoding & 0x01
  8.  
  9. bool visited;
  10. int parentRow;
  11. int parentCol;
  12. char direction; // From parent.
  13.  
  14.  
  15. } MazeCell;

Maze.cpp
  1. // Read and initialize all the cells.
  2. int x = 0;
  3. while(txtfile >> x)
  4. {
  5. for (int i = 0; i < rows; i++)
  6. {
  7. for (int j = 0; j < cols; j++)
  8. {
  9. maze[i][j].doorEncoding = x;
  10. }
  11. }
  12. }

Thank you, and am srry for the troubles once again.
Attached Files
File Type: txt Info.txt (1.3 KB, 16 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Stacks, Queues, and a Maze??

 
0
  #2
Feb 20th, 2008
Very shortly about the & operator, it is the bitwise and operator.
See e.g. here http://www.learncpp.com/cpp-tutorial...ise-operators/ for details.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Stacks, Queues, and a Maze??

 
0
  #3
Feb 20th, 2008
Hey thank you for the link, it really explained a lot.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Stacks, Queues, and a Maze??

 
0
  #4
Feb 21st, 2008
Am having troubles coming up with an implementation that would solve a maze. Am trying to picture how am going to have it the mouse examine the room to see which doors are available and determine the neighboring cells and determine if its been visited before and it it hasn't move to that direction then place the cell into the stack. This is obviously going to be done by a loop but i don't know how to go about starting it. What i got so far:

cpp
  1. void MazeCell::doorEncoding(unsigned short int x)
  2. {
  3. /* Cells are maped by chars.
  4.   1st bit maps West door
  5.   2nd bit maps south door
  6.   3rd bit maps east door
  7.   4th bit maps North door*/
  8.  
  9. //The 4 directions (North, South, East and West)
  10. enum {west, south, east, north};
  11.  
  12. //find the state of a door (state of door will be given by "x & 0x08")
  13. int north = x & 0x08;
  14. int east = x & 0x04;
  15. int south = x & 0x02;
  16. int west = x & 0x01;
  17. }
  18.  
  19. void Maze::Solve() //need help
  20. {
  21. CellPosition c;
  22. stack<CellPosition> cpos;
  23. c.row = mouseRow;
  24. c.col = mouseCol;
  25. cpos.push(c);
  26.  
  27. maze[mouseRow][mouseCol].parentRow = -1;
  28. maze[mouseRow][mouseCol].parentCol = -1;
  29.  
  30. bool visited = false;
  31. while(!cpos.empty())
  32. {
  33. while(maze[mouseRow][mouseCol] != maze[cheeseRow][cheeseCol])
  34. {
  35. if()
  36.  
  37.  
  38. visited = true;
  39. }
  40. }
  41. cout << "Maze has no solution" << endl;
  42. }


I would have the mouse move through each cell and with each iteration of the loop, take the cell and mark it as visited and push it into a stack. If it is the cheese cell, then stop the loop stops. If not, we continue picking a side with a doorway to the next cell position and continue doing this till it finds the cheese.


visited -- 1 ==> the cell has been visited. 0 => not visited.
parent -- parent (i.e. previous cell) if the cell has been visited.
For the first cell (mouse's initial position) there is no parent.
Hence the -1 values
direction -- The direction (i.e. the letter) from parent to this cell.

Attaching what i have so far if any1 wants to see the grand scheme of things.
Attached Files
File Type: cpp Maze-stk.cpp (2.2 KB, 41 views)
File Type: h Maze-stk.h (1.3 KB, 34 views)
File Type: cpp Mazetest.cpp (431 Bytes, 38 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: kjc367 is an unknown quantity at this point 
Solved Threads: 0
kjc367 kjc367 is offline Offline
Newbie Poster

Re: Stacks, Queues, and a Maze??

 
0
  #5
Feb 21st, 2008
One method to solve a maze is to consider the walls more as a door that is open (available) or closed (already tried). Boundaries are marks closed as are the solid walls.

the sequence would be:
1) Enter cell
2) If this cell contains cheese, stop -- maze solved
3) If you've been here before (i.e. visited), pop off the stack (i.e. return to the previous cell and repeat step 5)
4) Mark as visited
5) If the door to the North, East, West, or South is open, mark it as closed (i.e. now a wall) and move to that new cell. (i.e. push cell and move to next cell)
6) All paths checked, no solution

Using just a byte and using the upper nibble to mark used/closed doors:

existing
bit 0: Door to West
bit 1: Door to South
bit 2: Door to East
bit 3: Door to North

new
bit 4: Door to West Used
bit 5: Door to South Used
bit 6: Door to East Used
bit 7: Door to North Used

Using this approach, you can see that if any of the upper nibble is set, then this cell has been visited. To use a door, you check that it is present (bits 0-3) and unused (bits 4-7). If you're working on minimal space used, then you could even "mark" the cheese with a special code such as 0x00 (no doors, none used).

This also works well for a recursive calling. You could argue that the recursive is using a stack, it just happens to be the system stack.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stacks, Queues, and a Maze??

 
1
  #6
Feb 21st, 2008
Here's a slightly different algorirhm.
  1. Given:
  2.  
  3. 1) a fixed maze with:
  4. a) defined room for mouse to start and
  5. b) defined room for placement of cheese
  6. and
  7. 2) each room has:
  8. a) a flag for being visited, and
  9. b) 4 sides which may either be a wall or a door, and
  10. c) a variable to keep track of next side to look at
  11. (side numbers range from 1-4, with side number set to 1 on construction),
  12.  
  13. then one approach using iterative style might be something like this:
  14.  
  15. //special case
  16. designate that mouse starting room has been visited
  17. push mouse starting room on stack
  18.  
  19. //general case
  20. while stack not empty and maze not solved
  21. current room will be top of stack
  22. //check for solution
  23. if current room same as cheese room
  24. maze solved
  25. else if current room side number is 5 //means no more neighbors to look in
  26. pop current room from stack //go back to a prior room
  27. else //look for new room
  28. while(current room side number is less than 5 and need new room)
  29. if this side is a door
  30. new room is selected based on what side this is
  31. current room side number is incremented by 1
  32. if new room not visited
  33. need new room is false
  34. new room marked as visited
  35. push new room on stack
  36. else
  37. current room side number is incremented by 1

Warning: it's been a while since I've done maze solving programs so logic not infallible.
Last edited by Lerner; Feb 21st, 2008 at 7:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Stacks, Queues, and a Maze??

 
0
  #7
Feb 22nd, 2008
Thank you kjc367 and Lerner, the responses i've gotton from both of you as made the process of coming with a solution method clear but am having a hard time translating that to code....i've tried something but i don't think its right.
  1. void Maze::Solve() //need help
  2. {
  3. The 4 sides (North, South, East and West)
  4. enum {w, s, e, n};
  5.  
  6. CellPosition c;
  7. stack<CellPosition> cpos;
  8. maze[mouseRow][mouseCol].visited = true;
  9. maze[mouseRow][mouseCol].parentRow = -1;
  10. maze[mouseRow][mouseCol].parentCol = -1;
  11. c.row = mouseRow;
  12. c.col = mouseCol;
  13. cpos.push(c);
  14.  
  15.  
  16. bool visited = false;
  17. while(!cpos.empty())
  18. {
  19. //check for solution
  20. while(maze[mouseRow][mouseCol] != maze[cheeseRow][cheeseCol])
  21. {
  22. if(maze[mouseRow][mouseCol] == maze[cheeseRow][cheeseCol])
  23. {
  24. c.row = mouseRow;
  25. c.col = mouseCol;
  26. cpos.push(c);
  27. visited = true;
  28. cout<< "Maze has been solved!!" << endl;
  29. break;
  30. }
  31. else if (north == 8) // does this say north = binary value(1000)?
  32. {
  33. maze[mouseRow][mouseCol] = maze[i][j-1];
  34. maze[mouseRow][mouseCol].direction = n;
  35. maze[i][j-1].parentRow = maze[i];
  36. maze[i][j-1].parentCol = maze[j+1];
  37. c.row = mouseRow;
  38. c.col = mouseCol;
  39. cpos.push(c);
  40. if(north != 8 && east == 6)
  41. {
  42. maze[mouseRow][mouseCol] = maze[i+1][j];
  43. maze[mouseRow][mouseCol].direction = e;
  44. maze[i+1][j].parentRow = maze[i-1];
  45. maze[i+1][j].parentCol = maze[j];
  46. c.row = mouseRow;
  47. c.col = mouseCol;
  48. cpos.push(c);
  49. }
  50. if(north != 8 && east != 4 && south == 2)
  51. {
  52. maze[mouseRow][mouseCol] = maze[i][j+1];
  53. maze[mouseRow][mouseCol].direction = s;
  54. maze[i][j+1].parentRow = maze[i];
  55. maze[i][j+1].parentCol = maze[j-1];
  56. c.row = mouseRow;
  57. c.col = mouseCol;
  58. cpos.push(c);
  59. }
  60. if(north != 8 && east != 4 && south != 2 && west == 1)
  61. {
  62. maze[mouseRow][mouseCol] = maze[i-1][j];
  63. maze[mouseRow][mouseCol].direction = w;
  64. maze[i-1][j].parentRow = maze[i+1];
  65. maze[i-1][j].parentCol = maze[j];
  66. c.row = mouseRow;
  67. c.col = mouseCol;
  68. cpos.push(c);
  69. }
  70. }
  71. maze[mouseRow][mouseCol].visited = true;
  72.  
  73. if (north !=8 && east !=4 && south !=2 && west !=1)
  74. {
  75. // go back to parent cell and choose other available direction
  76. maze[mouseRow][mouseCol]=
  77. }
  78. }
  79. }
  80. cout << "Maze has no solution" << endl;
  81. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stacks, Queues, and a Maze??

 
0
  #8
Feb 22nd, 2008
I find it useful to separate the bitwise manipulations from
the algorithym as much as I can since bitwise manipulation
always confuses me.

I see no need for "parent" information, as the stack and
the visited variable will keep track of where you've been.

Likewise, direction is an unnecessary confounding variable,
in my opinion.

This is a first draft and not a compiled, tried and true version
of how I would implement the pseudocode from my first post.
There is undoubtedly one or more syntactic or logic error that
you will find, but it gives you an idea of how to go from
pseudocode to code.
  1. class Cell
  2. {
  3. public:
  4. int rowNumber;
  5. int colNumber;
  6. bool doors[4]; //true == door, false == wall
  7. bool visited; //set to false in constructor
  8. int sideNumber; //range 0-3, set to zero in constructor
  9. bool operator == //useful, but not necessary
  10. etc
  11. };
  12.  
  13. void Maze::initialize()
  14. {
  15. read data from file to establish:
  16. 1) mouse starting position
  17. 2) cheese location
  18. 3) Cells in the maze
  19. 4) values for sides of all Cells in maze
  20. }
  21.  
  22. void Maze::Solve()
  23. {
  24. Cell * newRoom;
  25. stack<Cell> path;
  26.  
  27. bool solutionNeeded = true;
  28. bool needNewRoom;
  29.  
  30. maze[mouseStartRow][mouseStartCol].visited = true;
  31. path.push(maze[mouseStartRow][mouseStartCol]);
  32.  
  33. while(!path.empty() && solutionNeeded)
  34. {
  35. //mouse is path.top();
  36.  
  37. if(path.top() == cheeseLocation)
  38. solutionNeeded = false;
  39. else if(path.top().sideNumber == 4) //dead end
  40. path.pop(); //go back to prior room.
  41. else
  42. {
  43. needNewRoom = true;
  44. while(path.top().sideNumber < 4 && needNewRoom)
  45. {
  46. if(path.top().doors[path.top().sideNumber]) //if this side is a door
  47. {
  48. switch(path.top().sideNumber)
  49. {
  50. case 0: //west side of current cell is a door, so move 1 col to left
  51. newRoom = &maze[path.top().rowNumber][path.top().colNumber - 1];
  52. break;
  53. //etc
  54. }
  55. path.top().sideNumber++;
  56. if(!newRoom->visited)
  57. {
  58. needNewRoom = false;
  59. newRoom->visited = true;
  60. path.push(*newRoom);
  61. }
  62. }
  63. else
  64. path.top().sideNumber++;
  65. }
  66. }
  67. }
  68. if(solutionNeeded)
  69. cout << "no solution" << endl;
  70. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC