Amazon OA2 12.18 due, Maze只过了16个, 求大神分析

原帖地址:一亩三分地

刚做完的OA2, 第一部分就不赘述了, coding遇到的第一题是reverse second half, 轻松愉快解决, 第二题是Maze, 只过了16个case, 虽然死定了, 然而还是想知道死在何处, 求大神帮看下code也算帮助后面的同学了~做之前也参考了不少地里的解法, 和下面的解法类似的还不少, 中途换用DFS写还是只过了16个class Point {
int row;
int col;

Point(int row, int col) {
this.row = row;
this.col = col;
}
}

public class Maze {
public static int solution(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
if (matrix[0][0] == 9) {
return 1;
}
if (matrix[0][0] == 0) {
return 0;
}
final int[] dx = { -1, 0, 0, 1 };
final int[] dy = { 0, 1, -1, 0 };
int m = matrix.length;
int n = matrix[0].length;
Queue q = new LinkedList();
matrix[0][0] = -1;
q.add(new Point(0, 0));
while (!q.isEmpty()) {
Point curPoint = q.poll();
for (int i = 0; i < 4; i++) { int nextRow = curPoint.row + dx[i]; int nextCol = curPoint.col + dy[i]; if (nextRow >= 0 nextRow < m nextCol >= 0 nextCol < n) { if (matrix[nextRow][nextCol] == 9) { return 1; } else if (matrix[nextRow][nextCol] == 1) { q.add(new Point(nextRow, nextCol)); matrix[nextRow][nextCol] = -1; } } } } return 0; } }复制代码 补充内容 (2015-12-16 22:09): 楼主已经收到了拒信, Amazon真够效率的, 晚上9点发的消息