You are given two integers m and n, representing the number of rows and columns of a grid.
Construct any m x n grid consisting only of the characters '.' and '#', where:
'.' represents a free cell.'#' represents an obstacle cell.A valid path is a sequence of free cells that:
(0, 0).(m - 1, n - 1).(i, j) to (i, j + 1), or(i, j) to (i + 1, j).Return any grid such that there is exactly one valid path from the top-left cell to the bottom-right cell.
Example 1:
Input: m = 2, n = 3
Output: ["..#","#.."]
Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2)
Example 2:
Input: m = 3, n = 3
Output: ["..#","#..","##."]
Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2) → (2,2)
Example 3:
Input: m = 1, n = 4
Output: ["...."]
Explanation:
The only valid path is: (0,0) → (0,1) → (0,2) → (0,3)
Constraints:
1 <= m, n <= 25Problem Overview: You need to construct an m x n grid such that there is exactly one valid path from the top-left cell to the bottom-right cell. Movement is typically restricted to right or down, so the grid must block all alternative routes while keeping one continuous path.
Approach 1: Brute Force Grid Search (Backtracking) (Time: exponential, Space: O(m*n))
One theoretical approach is to generate different grid configurations of open and blocked cells and run a path counting algorithm such as DFS or DP to verify whether exactly one path exists. For every configuration, you run a traversal from (0,0) and count ways to reach (m-1,n-1). This quickly becomes infeasible because the number of possible grids grows exponentially with m*n. It mainly serves as a conceptual baseline showing the requirement: all alternative routes must be blocked.
Approach 2: Deterministic Grid Construction (Time: O(m*n), Space: O(1))
The practical solution is to construct the grid so only a single path is physically possible. A simple pattern is to keep the entire first row open and the entire last column open, while blocking all other cells. Starting from (0,0), the only possible movement is to go right across the first row until the last column, then move down to the bottom-right corner. Any attempt to move down earlier hits a blocked cell, eliminating alternative paths.
This guarantees exactly one valid route while keeping the implementation trivial. You initialize the grid, mark cells along the chosen path as passable, and block the rest. The algorithm only iterates through the grid once to assign values, so the runtime is O(m*n) with constant extra memory.
Conceptually, the problem relates to counting paths in a grid, commonly solved using dynamic programming. Instead of computing the number of paths, you design the grid so the count becomes exactly one. The construction also resembles a simple greedy strategy where you enforce a single deterministic route through a matrix.
Recommended for interviews: The deterministic construction approach. Interviewers expect you to recognize that multiple paths arise from branching choices. By blocking every branch except one straight route, you guarantee a unique path with a very simple O(m*n) construction. Mentioning brute force or DP path counting briefly shows understanding of the underlying grid path problem, but the direct construction demonstrates stronger problem-solving instincts.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Grid Enumeration + DFS Path Count | Exponential | O(m*n) | Conceptual understanding of how unique paths emerge |
| Dynamic Programming Path Counting (Validation) | O(m*n) | O(m*n) | When verifying the number of paths in an existing grid |
| Deterministic Grid Construction | O(m*n) | O(1) | Best approach to directly build a grid with exactly one path |
Practice Create Grid With Exactly One Path with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor