[LeetCode] Create Grid With Exactly One Path

3963. Create Grid With Exactly One Path

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:

  • Starts at the top-left cell (0, 0).
  • Ends at the bottom-right cell (m - 1, n - 1).
  • Moves only:
    • Right, from (i, j) to (i, j + 1), or
    • Down, from (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.

1
2
3
4
5
6
7
8
9
class Solution {
public:
vector<string> createGrid(int n, int m) {
vector<string> res(n, string(m,'#'));
for(int i = 0; i < n; i++) res[i][0] = '.';
for(int i = 0; i < m; i++) res[n-1][i] = '.';
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/create-grid-with-exactly-one-path/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.