Below is my code:
It run into some issues with test case:
[[0,0,0],[0,0,0],[0,0,0]]
[0,0]
[1,2]
It is obviously that the ball can reach the destination with path:
[0.0] -> [1,0] -> [1,1] -> [1,2]
Why the test case expect false in above testcase?
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
Queue<int[]> q = new LinkedList<int[]>();
q.add(start);
int[] temp = new int[2];
int x_index;
int y_index;
int[][] direction = {{1,0},{-1,0},{0,1},{0,-1}};
while(!q.isEmpty()){
int size = q.size();
for(int i=0;i<size;i++){
temp = q.poll();
x_index = temp[0];
y_index = temp[1];
if(maze[x_index][y_index] == 1) continue;
else{
maze[x_index][y_index] = 2;
for(int j = 0;j<direction.length;j++){
int x_index_temp = x_index + direction[j][0];
int y_index_temp = y_index + direction[j][1];
if(x_index_temp >=0 && x_index_temp<maze.length && y_index_temp >=0 && y_index_temp <maze[0].length ){
if(maze[x_index_temp][y_index_temp] != 1) {
int[] added = new int[2];
added[0] = x_index_temp;
added[1] = y_index_temp;
q.add(added);
if(x_index_temp == destination[0] && y_index_temp == destination[1]) {
x_index_temp += + direction[j][0];
y_index_temp += + direction[j][1];
if(x_index_temp >=0 && x_index_temp<maze.length && y_index_temp >=0 && y_index_temp <maze[0].length ){
if(maze[x_index_temp][y_index_temp] == 1) return true;
}else{
return true;
}
}
}
}
}
}
}
}
return false;
}
}