VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-grid-paths/

⇱ CSES Solutions - Grid Paths - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Grid Paths

Last Updated : 23 Jul, 2025

There are 88418 paths in a 7x7 grid from the upper-left square to the lower-left square. Each path corresponds to a 48-character description consisting of characters D (down), U (up), L (left) and R (right). You are given a description of a path which may also contain characters ? (any direction). Your task is to calculate the number of paths that match the description.

Example

Input: ??????R??????U??????????????????????????LD????D?

Output: 201

Explanation: There are 201 possible paths with the given description.

Approach using DFS:

The idea is to use DFS and try all four directions if the current character in the string is ‘?’ else we go in direction given (L,R,U,D).
Pruning/Terminating the search in order to optimize the code:
If we cannot go up or down but can turn either left or right or if we cannot turn left or right but can go up and down, in this case the grid splits into two parts. It is clear that we cannot visit one part of the grid, so we can terminate the search. These four cases also leaves unvisited squares which can cannot be visited. Hence, the search needs to terminate.

  • If the upper-right diagonal square is visited and the up and right squares are unvisited.
  • If the lower-right diagonal square is visited and the down and right squares are unvisited.
  • If the upper-left diagonal square is visited and the up and left squares are unvisited.
  • If the lower-left diagonal square is visited and the down and right squares are unvisited.

Step-by-step algorithm:

  • Define a function countPaths that takes the current position (x, y) and the current position in the path description string pos as parameters.
  • Base Case Return 1:
    • if all characters in the string have been processed and if the current position is the lower-left square else 0.
  • Base Case Return 0:
    • If the current cell has already been visited.
    • If the current position is the lower-left square before processing all characters.
    • If the current position is such that the up and down squares are unvisited and the left and right squares are visited.
    • If the current position is such that the left and right squares are unvisited and the up and down squares are visited.
    • If the current position is such that the upper-right diagonal square is visited and the up and right squares are unvisited.
    • If the current position is such that the lower-right diagonal square is visited and the down and right squares are unvisited.
    • If the current position is such that the upper-left diagonal square is visited and the up and left squares are unvisited.
    • If the current position is such that the lower-left diagonal square is visited and the down and left squares are unvisited.
  • Mark the current cell as visited.
  • Initialize a variable to store the number of paths.
  • Check if the current character in the string is ‘?’. If true, try all four directions.
  • Check if the current character in the string is a direction (L,R,U,D). If true, go in that direction.
  • Unmark the current cell.
  • Return the number of paths.

Below is the implementation of the approach:


Output
201

Time Complexity: O(4N), where N is the length of string.
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: