Download presentation
Presentation is loading. Please wait.
Published byMariah Webster Modified over 8 years ago
1
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day
2
Problem Solving Using a Queue: Solving Mazes When we reach branches in the path we use a queue to keep track of all the paths (i.e. the possible solutions) We also need to be careful not to backtrack –Use a second boolean array ( beenThere )
3
Java Code: The Program Class pub public class QSearch { { private static final int MAX_COORD = 10; private class Position { public int r, // Row coordinate c; // Column coordinate } // class Position private Queue posQueue = new ListQueue (); // Queue of positions still to be checked private boolean[][] maze = new boolean[MAX_COORD][MAX_COORD]; private boolean[][] beenThere = new boolean[MAX_COORD][MAX_COORD]; // Keep track of previous positions...
4
The addPosition Method pub public void addPosition (int row, int col) // Put // Put a new position on the queue of positions { Position p = new Position(); p.r = row; p.c = col; posQueue.add(p); } // addPosition
5
solveMaze : Initialisation pub public void solveMaze () { { int r, c = 0; // Row and column coordinates readMaze("MAZE"); // Find starting position for (r = 0; r < MAX_COORD; r++) if (maze[r][0]) // r is starting row break; // Put starting position on queue addPosition(r, 0);... } // solveMaze
6
solveMaze : Main Loop whi while (! posQueue.isEmpty()) { { Position nextPos; // Remove next position from queue and try // all possible moves nextPos = posQueue.remove(); c = nextPos.c; r = nextPos.r; beenThere[r][c] = true; // Note we visited // this spot if (c == MAX_COORD-1) // Found exit, so leave search loop break; // Try all possible moves from this position... } // while
7
s solveMaze : Exploring Positions // Try to move up if (maze[r-1][c] && ! beenThere[r-1][c]) addPosition(r-1, c); // Try to move right if (maze[r][c+1] && ! beenThere[r][c+1]) addPosition(r, c+1); // Try to move down if (maze[r+1][c] && ! beenThere[r+1][c]) addPosition(r+1, c); // Try to move left if (c > 0 && maze[r][c-1] && ! beenThere[r][c-1]) addPosition(r, c-1); Don’t walk back out the entrance!
8
Summary Problem Solving with a Queue Repeatedly: –Take position/state from queue –Add to queue all new positions/states that can be reached from it Until solution is reached
9
Changing the Data Structure? The queue is only used to keep track of paths that still need to be explored What happens if we use a stack? Depth-first search –The same algorithm will search the full extent of one path before returning to consider the others
10
Tree View (0, 2) (1, 2) (1, 1)(2, 2)(1, 3) (3, 2)(1, 4) (4, 2)(2, 4)... Breadth-First Search
11
Tree View (0, 2) (1, 2) (1, 1)(2, 2)(1, 3) (3, 2)(1, 4) (4, 2)(2, 4)... Depth-First Search
12
May find a solution faster than the breadth- first search But: It may not be the optimal solution (shortest path)
13
Problem Solved!
14
Deques Double-ended queue Items can be added and removed at either end Refer to “left end” and “right end” Deque
15
Implementation Techniques Need to be able to access the list from either end: –doubly-linked list leftright cba
16
Implementation Techniques (cont.) Easily converted to a circularly-linked list leftright cba
17
Implementation Techniques (cont.) Don’t need both “end” pointers left cba
18
Implementation Techniques (cont.) Introduce a list-head node (or header node) head cba
19
Implementation Techniques (cont.) An empty doubly-, circularly-linked list with a header node: head
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.