Data Structures and Algorithms - Assignment 5 | COP 3530, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/18/2009

koofers-user-7rn
koofers-user-7rn 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP 3530
Assignment 5
Due: 29th Sep 2008
In this problem you will solve the “Rat in a maze problem” (Lecture 12), using Stacks
and Queues.
The maze is given as a 2D array, where '0's are squares the rat can move to; '1's represent
the squares where the rat cannot move to. The coordinates of the square at the top left
corner are (0,0). The coordinates of the square at the bottom right corner are (14,12). The
positive direction of x axis is to the right, and the positive direction of y axis is down.
(0,0)
000100000010000
000100100000000
000000011111111
000111001001000
000001100100100
110001100100000
011001100100000
001001100100000
011001000000000
001000111111111
001010000000000
001010001000000
000010010000000
(14,12)
You have to implement three methods:
searchStack(int fromX,int fromY,int toX,int toY) ,
searchQueue(int fromX,int fromY,int toX,int toY), and
searchStackSmart(int fromX,int fromY,int toX,int toY), which
find a path from (fromX,fromY) to (toX,toY), simulating the actions of a rat using
the following strategies:
searchStack: A rat that always attempts to move in a specific order: right, down, left,
up. This method should be implemented using a stack.
searchQueue: On a given square, the rat reproduces, and up to four rats take one step
away from the current square, each rat in a different direction. Each of these rats will
choose one direction: right, down, left and up. This method should be implemented using
a queue.
pf3
pf4
pf5

Partial preview of the text

Download Data Structures and Algorithms - Assignment 5 | COP 3530 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

COP 3530

Assignment 5

Due: 29th^ Sep 2008

In this problem you will solve the “Rat in a maze problem” (Lecture 12), using Stacks

and Queues.

The maze is given as a 2D array, where '0's are squares the rat can move to; '1's represent

the squares where the rat cannot move to. The coordinates of the square at the top left

corner are (0,0). The coordinates of the square at the bottom right corner are (14,12). The

positive direction of x axis is to the right, and the positive direction of y axis is down.

You have to implement three methods:

searchStack(int fromX,int fromY,int toX,int toY) ,

searchQueue(int fromX,int fromY,int toX,int toY), and

searchStackSmart(int fromX,int fromY,int toX,int toY), which

find a path from (fromX,fromY) to (toX,toY), simulating the actions of a rat using

the following strategies:

searchStack: A rat that always attempts to move in a specific order: right, down, left,

up. This method should be implemented using a stack.

searchQueue: On a given square, the rat reproduces, and up to four rats take one step

away from the current square, each rat in a different direction. Each of these rats will

choose one direction: right, down, left and up. This method should be implemented using

a queue.

searchStackSmart: A rat that first attempts to move in the general direction of the exit,

and moves away from the exit only when moving towards the exit has failed. For

example, if the exit is down and right from the rat, the order of the first two directions it

will attempt will be either “down, then right” or “right, then down”. This method should

also be implemented using stacks.

The resulting output should be like this:

I've traveled through 78 square(s). The path contains 45 square(s)

where '2's are squares on the resulting path. If there is a path available, your method

should return true and your program should output the path it found. If there is no path

available, your method should return false. You don’t have to find all the paths.

The distance the rat travels is equal to the number of stack (queue) pop actions your

methods take during their execution.

The template for the class is given below:

package dataStructures;

import java.util.Vector;

public class RatInMaze {

int [][] maze_;

int width_; int height_;

ArrayStack stack_; //You must use this variable to ref your stack ArrayQueue queue_; //You must use this variable to ref your queue

output += "'ve traveled through " + traveled_ + "square(s). The path contains " + pathLength_ + "square(s)"; System.out.println(output); } else { System.out.println("I cannot get there."); }

public boolean searchStack( int fromX,int fromY,int toX,int toY) { stack_= new ArrayStack(); queue_ = null ; //Your code goes here. } public boolean searchStackSmart( int fromX,int fromY,int toX,int toY) { stack_= new ArrayStack(); queue_ = null ; //Your code goes here. } public boolean searchQueue( int fromX,int fromY,int toX,int toY) { stack_= null ; queue_ = new ArrayQueue(); //Your code goes here. }

// Please use the test code as follows. public static void main(String[] args) { RatInMaze rim = new RatInMaze(); Vector maze = new Vector();

maze.add("000100000010000"); maze.add("000100100000000"); maze.add("000000011111111"); maze.add("000111001001000"); maze.add("000001100100100"); maze.add("110001100100000"); maze.add("011001100100000"); maze.add("001001100100000"); maze.add("011001000000000"); maze.add("001000111111111"); maze.add("001010000000000"); maze.add("001010001000000"); maze.add("000010010000000");

System.out.println("\n\n");

rim.load(maze); rim.print(rim.searchQueue(-1,1,10,10)); rim.load(maze);

rim.print(rim.searchStack(0,0,41,1));

int fromX = 0; int fromY = 7; int toX = 14; int toY = 6; System.out.println("\n\n"); rim.load(maze); System.out.println("A rat is searching:"); rim.print(rim.searchStack(fromX,fromY,toX,toY));

System.out.println("\n\n"); rim.load(maze); System.out.println("Multiple rats are searching:"); rim.print(rim.searchQueue(fromX,fromY,toX,toY));

System.out.println("\n\n"); rim.load(maze); System.out.println("A smart rat is searching:"); rim.print(rim.searchStackSmart(fromX,fromY,toX,toY));

Note:

In int [][] maze, the first index is the y coordinate. The second index is the x coordinate.

The given load and print method rely on this assumption.

You are allowed to call any method in the ArrayStack class and the ArrayQueue class.

(a) Write Java code for the method searchStack, you must use the stack.

(b) Write Java code for the method searchQueue, you must use the queue.

(c) Write Java code for the method searchStackSmart, you must use the stack.

(d) Compare the length of the found path and numbers of squares these rats travel

through. List your result and comment on what you find. Which strategy is better?