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

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

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)

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

View Full Document
{ //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 {
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);

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

View Full Document
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
Ask a homework question - tutors are online