sol6 - COP 3530 HW6 - Solutions a) package dataStructures;...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3530 HW6 - Solutions a) package dataStructures; import java.util.Vector; public class RatInMaze { int [][] maze_ ; int width_ ; int height_ ; ArrayStack stack_ ; ArrayQueue queue_ ; int traveled_ ; int pathLength_ ; private boolean isValid( int x, int y) { if ((x < 0)||(x >= width_ )) { return false ; } if ((y < 0)||(y >= height_ )) { return false ; } if ( maze_ [y][x] > 0) { return false ; } return true ; } private boolean search( int fromX, int fromY, int toX, int toY, boolean smart) { traveled_ = 0; pathLength_ = 0; if (!isValid(fromX,fromY)) { System. out .println( "Starting point is not inside the maze!" ); return false ; } if (!isValid(toX,toY)) { System. out .println( "Ending point is not inside the maze!" ); return false ; } push(2,fromX,fromY); while (!isEmpty()) { int pos = (Integer)pop(); traveled_ ++; int x = pos% width_ ; int y = pos/ width_ ; if ((x == toX)&&(y == toY)) { break ; } if (!smart)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ //Not a smart rat? push(6,x,y); push(4,x,y); push(5,x,y); push(3,x,y); } else { //A smart rat! int dx = x - toX; int dy = y - toY; if (dx == 0) { if (dy < 0) { push(6,x,y); } else { push(5,x,y); } push(3,x,y); push(4,x,y); if (dy < 0) { push(5,x,y); } else { push(6,x,y); } } else if (dy == 0) { if (dx <= 0) { push(4,x,y); } else { push(3,x,y); } push(5,x,y); push(6,x,y); if (dx <= 0) { push(3,x,y); } else { push(4,x,y); } } else {
Background image of page 2
if ( dx < 0 ) push(4,x,y); if ( dx > 0 ) push(3,x,y); if ( dy < 0 ) push(6,x,y); if ( dy > 0 ) push(5,x,y); if ( dx < 0 ) push(3,x,y); if ( dx > 0 ) push(4,x,y); if ( dy < 0 ) push(5,x,y); if ( dy > 0 ) push(6,x,y); } } // print(true);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COP 3530 taught by Professor Davis during the Fall '08 term at University of Florida.

Page1 / 7

sol6 - COP 3530 HW6 - Solutions a) package dataStructures;...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online