#K35072. Count Paths in a Grid
Count Paths in a Grid
Count Paths in a Grid
You are given an m x n grid. Your task is to determine the number of distinct paths from the top-left corner to the bottom-right corner if you can only move right or down at any step.
The problem can be solved using dynamic programming. Let \(dp[i][j]\) denote the number of ways to reach cell \((i, j)\). The recurrence relation is given by:
\( dp[i][j] = dp[i-1][j] + dp[i][j-1] \)
with the base cases that all cells in the first row and first column have exactly one way to be reached.
For example:
- For a 3x3 grid, the number of paths is 6.
- For a 2x2 grid, the number of paths is 2.
Write a program that reads the grid dimensions from standard input and prints the number of unique paths to standard output.
inputFormat
The input consists of two space-separated integers m and n (1 \le m, n \le 100
), representing the number of rows and columns of the grid, respectively. The input is read from standard input (stdin).
outputFormat
The output is a single integer representing the number of distinct paths from the top-left corner to the bottom-right corner in the grid. This should be printed to standard output (stdout).
## sample3 3
6