1005. Find if Path Exists in Grid

EasyGraphsGridDFSBFS

Given a grid where 0 is an open cell and 1 is a wall, and given source and destination cells [r, c], determine whether there is a path from source to destination moving 4-directionally through open cells only. Return true or false. The input is JSON {grid, source, destination}.

Input: JSON {grid, source, destination}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"grid":[[0,0,0],[1,1,0],[0,0,0]],"source":[0,0],"destination":[2,2]}
Output: true
Explanation: A clear path exists.
Example 2
Input: {"grid":[[0,1],[1,0]],"source":[0,0],"destination":[1,1]}
Output: false
Explanation: Blocked by walls.
Example 3
Input: {"grid":[[0]],"source":[0,0],"destination":[0,0]}
Output: true
Explanation: Same cell.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →