patternjavaMinor
Maze problem cleanup and optimization
Viewed 0 times
problemmazecleanupoptimizationand
Problem
Maze, assumption - single point of entry and a single point of exit. Also directions to travel in maze are North South East West. Request for optimization and code cleanup.
```
final class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (getClass() != o.getClass())
return false;
// now to the check.
final Coordinate coordinate = (Coordinate) o;
return coordinate.x == x && coordinate.y == y;
}
public int hashCode() {
return x + y;
}
}
public class Maze {
private final int[][] maze;
public Maze(int[][] maze) {
if (maze == null) {
throw new NullPointerException("The input maze cannot be null");
}
if (maze.length == 0) {
throw new IllegalArgumentException("The size of maze should be greater than 0");
}
this.maze = maze;
}
public Set solve() {
Set setCoordinate = new LinkedHashSet();
for (int j = 0; j set) {
assert set != null;
if (set.contains(new Coordinate(row, col))) return false;
if ((((row == 0) || (row == maze.length - 1)) || ((col == maze[0].length - 1) || col == 0)) && !set.isEmpty() / discard the entry tile /) {
set.add(new Coordinate(row, col));
return true;
}
set.add(new Coordinate(row, col));
boolean getMaze = false;
/**
* travel in all 4 language
*/
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze
```
final class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (getClass() != o.getClass())
return false;
// now to the check.
final Coordinate coordinate = (Coordinate) o;
return coordinate.x == x && coordinate.y == y;
}
public int hashCode() {
return x + y;
}
}
public class Maze {
private final int[][] maze;
public Maze(int[][] maze) {
if (maze == null) {
throw new NullPointerException("The input maze cannot be null");
}
if (maze.length == 0) {
throw new IllegalArgumentException("The size of maze should be greater than 0");
}
this.maze = maze;
}
public Set solve() {
Set setCoordinate = new LinkedHashSet();
for (int j = 0; j set) {
assert set != null;
if (set.contains(new Coordinate(row, col))) return false;
if ((((row == 0) || (row == maze.length - 1)) || ((col == maze[0].length - 1) || col == 0)) && !set.isEmpty() / discard the entry tile /) {
set.add(new Coordinate(row, col));
return true;
}
set.add(new Coordinate(row, col));
boolean getMaze = false;
/**
* travel in all 4 language
*/
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze
Solution
I would say that this part of your code actually is a bit of unnecessary code duplication:
I think that you could use a direction enum, loop through the directions and then for each direction you can check:
Using the same
You can replace the code snippet with:
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}I think that you could use a direction enum, loop through the directions and then for each direction you can check:
- Is the coordinate in the current direction within the bounds? (You can use one method to check all four bounds)
- If it is, call
getMazePathfor then new coordinate.
Using the same
Direction4 enum as in the linked answer above,public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}You can replace the code snippet with:
for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}Code Snippets
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}Context
StackExchange Code Review Q#33303, answer score: 3
Revisions (0)
No revisions yet.