Thanks.
public class Maze
{
private char[][] m;
public Maze(char [][]l)
{
m = l;
}
public boolean escape(int r, int c)
{
boolean done = false;
if (valid(r,c))
{
char x;
x = m[r][c];
if (r == 0 && c == 1)
done = true;
else
{
done = escape (r+1, c);
if (!done && (x != '*' || x == '0'))
done = escape(r, c+1);
m[r][c] = '0';
if(!done && (x != '*' || x == '0'))
done = escape(r-1, c);
m[r][c] = '0';
if(!done && (x != '*' || x == '0'))
done = escape(r, c-1);
m[r][c] = '0';
}
if(done)
done = true;
}
return done;
}
private boolean valid(int r, int c)
{
boolean result = false;
if (r >= 0 && r < m.length-1 && c >= 0 && c < m[r].length-1)
result = true;
return result;
}
}
public class MazeTester
{
public static void main(String args[])
{
char[][] m =
{
{'*', ' ', '*', '*', '*', '*', '*', '*', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'},
{'*', ' ', '*', '*', '*', '*', '*', ' ', '*'},
{'*', ' ', '*', ' ', '*', ' ', ' ', ' ', '*'},
{'*', ' ', '*', ' ', '*', '*', '*', ' ', '*'},
{'*', ' ', ' ', ' ', '*', ' ', ' ', ' ', '*'},
{'*', '*', '*', ' ', '*', '*', '*', '*', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', '*', ' ', '*'},
{'*', '*', '*', '*', '*', '*', '*', ' ', '*'}
};
Maze maze = new Maze(m);
System.out.println(maze.escape(4,3));
System.out.println("Expected: true");
}
}
Edited by WingedPanther, 09 December 2008 - 08:36 AM.
add code tags (the # button)


Sign In
Create Account

Back to top









