class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int nr = maze.length;
int nc = maze[0].length;
boolean[][] visited = new boolean[nr][nc];
int[][] dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
Queue<int[]> queue = new LinkedList<>();
queue.add(start);
visited[start[0]][start[1]] = true;
while (!queue.isEmpty()) {
int[] idx = queue.poll();
if (idx[0] == destination[0] && idx[1] == destination[1]) {
return true;
}
for (int[] dir : dirs) {
int x = idx[0] + dir[0];
int y = idx[1] + dir[1];
while (x >= 0 && y >= 0 && x < nr && y < nc &&
maze[x][y] == 0) {
x += dir[0];
y += dir[1];
}
if (!visited[x - dir[0]][y - dir[1]]) {
queue.add(new int[] {x - dir[0], y - dir[1]});
visited[x - dir[0]][y - dir[1]] = true;
}
}
}
return false;
}
}
Replies to Re: Untitled
Title |
Name |
Language |
UNIX |
When |
Re: Re: Untitled |
Thundering Mousedeer |
text |
1620808177 |
2 Years ago. |
{"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"}