# Shortest Cell Path

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/3052/shortest-cell-path
Source observed: 2026-11-05T00:28:05.418Z
Last checked: 2026-11-05T00:28:05.418+00:00
Indexed: 2026-11-06T04:31:22.905+00:00

---

# Shortest Cell Path

**Difficulty:** Medium

[Unlock detailed company statistics for this question](https://www.aced.io/upgrade?src=questions)

## Problem

Given a `grid` containing `0`s and `1`s, a starting row and column `sr, sc`, and a target row and column `tr, tc`, return the length of the shortest path from `sr, sc` to `tr, tc` that traverses only cells whose value is `1`.

Every location in the path, including the start and end, must contain `1`. Each next location must be 4-directionally adjacent to the previous location. It is guaranteed that:

- `grid[sr][sc] = 1`
- `grid[tr][tc] = 1`
- The starting and target positions are different

If no valid path exists, return `-1`.

## Examples

### Reachable target

```text
grid = [
  [1, 1, 1, 1],
  [0, 0, 0, 1],
  [1, 1, 1, 1]
]
sr = 0, sc = 0, tr = 2, tc = 0
output = 8
```

Grid representation:

```text
1111
0001
1111
```

### Unreachable target

```text
grid = [
  [1, 1, 1, 1],
  [0, 0, 0, 1],
  [1, 0, 1, 1]
]
sr = 0, sc = 0, tr = 2, tc = 0
output = -1
```

Grid representation:

```text
1111
0001
1011
```

## Practice controls

We provide Question, Solution, and Community views, plus hints, an editor, sharing, output, test results, and a console. The question is categorized under [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms) and [Coding](https://www.aced.io/questions?type=coding). The question metadata shows one interview and reports that it was last asked 3 years ago.

## Related questions

- [Find the shortest path in a graph.](https://www.aced.io/questions/2094/find-shortest-path-graph)

[Sign up to run code](https://www.aced.io/signup?src=lesson)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3052%2Fshortest-cell-path&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3052%2Fshortest-cell-path). Opening a form does not execute a tool.