> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/07-15-2022-1091.-shortest-path-in-binary-matrix.md).

# 07/15/2022 1091. Shortest Path in Binary Matrix

Method: use bfs. Add the first node {0,0} to the queue and marked it as visited and set the distance = 1. While queue is not empty, get the size the queue and iterate it and poll the node. if the node index is the same with the end index, we return distance. Otherwise, we check the 8 direction of this node and add the appropriate index into the queue and mark it as visited. Outside the for loop, increment distance.

Time O(N)

Space O(N)
