1006. Count Reachable Nodes from a Source

EasyGraphsGridBFSDFS

Given a grid where 0 is an open cell and 1 is a wall, and a source cell [r, c], return the number of open cells reachable from the source (including the source itself) moving 4-directionally through open cells. If the source is a wall, return 0. The input is JSON {grid, source}.

Input: JSON {grid, source}.

Output: Integer — the number of reachable open cells.

Examples

Example 1
Input: {"grid":[[0,0,1],[1,0,1],[0,0,0]],"source":[0,0]}
Output: 6
Explanation: Six open cells are reachable.
Example 2
Input: {"grid":[[1]],"source":[0,0]}
Output: 0
Explanation: Source is a wall.
Example 3
Input: {"grid":[[0]],"source":[0,0]}
Output: 1
Explanation: Only the source.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →