class Solution {
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int nr = maze.length;
int nc = maze[0].length;
int[][] distances = new int[nr][nc];
for (int[] row : distances) {
Arrays.fill(row, Integer.MAX_VALUE);
}
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
Queue<int[]> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int[] curr = queue.poll();
for (int[] dir : dirs) {
int x = curr[0] + dir[0];
int y = curr[1] + dir[1];
int count = 0;
while (x >= 0 && x < nr &&
y >= 0 && y < nc &&
maze[x][y] != 1) {
x += dir[0];
y += dir[1];
count++;
}
if (distances[curr[0]][curr[1]] + count < distances[x-dir[0]][y-dir[1]]) {
distances[x-dir[0]][y-dir[1]] = distances[curr[0]][curr[1]] + count;
queue.add(new int[]{x - dir[0], y - dir[1]});
}
}
}
return distances[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distances[destination[0]][destination[1]];
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}