# The Maze

![](https://gblobscdn.gitbook.com/assets%2F-MQQ_aKuDz0ArQY9xrTi%2F-MQQ_hCHEF_NfTLb7c5f%2F-MQQhK6lAaBSDTrKohkL%2FWechatIMG22.png?alt=media\&token=90bc890e-7a68-4f04-84cc-c23bbda959bd)

```java
/*
 本算法答案由上岸科技提供。
 上岸科技是一个专致力于高效培养北美留学生拥有实际面试能力的团体。
 我们采用小班化线上，线下教学让学生更快，更好的学习到想要的知识。
 团队主要由一群怀揣情怀于美国高校毕业的一线IT公司工程师构成。
 我们坚信对于求职者算法并不是全部，合理的技巧加上适当的算法培训能够大大的提升求职成功概率也能大大减少刷题的痛苦。
 正如我们的信仰：我们教的是如何上岸而不仅是算法。
 更多信息请关注官网：https://www.shanganonline.com/
*/
class Solution {
    int[] xBias = {0, 0, 1, -1};
    int[] yBias = {1, -1, 0, 0};
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        if (maze[start[0]][start[1]] == 1) {
            return false;
        }
        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new ArrayDeque<>();
        queue.offer(start[0] * 100 + start[1]);
        int dest = destination[0] * 100 + destination[1];
        int m = maze.length;
        int n = maze[0].length;
        
        while (!queue.isEmpty()) {
            int pos = queue.poll();
            if (pos == dest) {
                return true;
            }
            visited.add(pos);
            int row = pos / 100;
            int col = pos % 100;
            for (int i = 0; i < 4; i++) {
                int prevRow = row;
                int prevCol = col;
                while (isValid(prevRow, prevCol, m, n) && maze[prevRow][prevCol] == 0) {
                    prevRow += xBias[i];
                    prevCol += yBias[i];
                }
                prevRow -= xBias[i];
                prevCol -= yBias[i];
                int newDest = prevRow * 100 + prevCol;
                if (isValid(prevRow, prevCol, m, n) && !visited.contains(newDest)) {
                    queue.offer(newDest);
                }
            }
        }
        return false;
    }
    
    private boolean isValid(int newRow, int newCol, int r, int n) {
        return newRow >= 0 && newRow < r && newCol >= 0 && newCol < n;
    }
}
```

![](https://gblobscdn.gitbook.com/assets%2F-MQQ_aKuDz0ArQY9xrTi%2F-MQQ_hCHEF_NfTLb7c5f%2F-MQQhNaEDp_XdzFm0Ypp%2FWechatIMG23.png?alt=media\&token=0eec3fd8-70e7-4ac4-93e9-6cec19015bdb)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shangan.gitbook.io/gong-kai-ke/di-er-zhang-bfs/ke-hou-lian-xi/the-maze.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
