Search in a Grid
Intro
A grid is essentially a lattice. The edges normally are undirected and have cost of 1. BFS normally gets the job done.
Auxiliary Functions
Define Position
private class Pos {
int row;
int col;
public Pos(int row, int col) {
this.row = row;
this.col = col;
}
}
Search Directions
int[] drow = new int[] {0, -1, 0, 1};
int[] dcol = new int[] {-1, 0, 1, 0};
Check inbound
private boolean inBound(char[][] grid, Pos pt) {
int m = grid.length;
int n = grid[0].length;
if (pt.row < 0 || pt.row >= m) {
return false;
}
if (pt.col < 0 || pt.col >= n) {
return false;
}
return true;
}
Last updated