#C11499. Unique Paths with Target Sum
Unique Paths with Target Sum
Unique Paths with Target Sum
You are given an integer matrix and a target sum \(T\). Starting from the top-left corner, you can only move either right or down. Your task is to find all unique paths such that the sum of the integers along the path equals \(T\). The path begins at the top-left cell and can end at any cell as soon as the cumulative sum is exactly \(T\). If no such path exists, output []
.
Note: The sum of a path \(P = [a_1, a_2, \dots, a_k]\) must satisfy \(\sum_{i=1}^{k} a_i = T\). Paths are formed by sequentially adding matrix elements along valid moves.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains two integers \(m\) and \(n\), representing the number of rows and columns of the matrix.
- The next \(m\) lines each contain \(n\) integers separated by spaces, representing the matrix.
- The last line contains a single integer \(T\), the target sum.
outputFormat
Output the found paths to standard output (stdout). Each path should be printed on a separate line, with the numbers separated by a single space. If no valid path exists, output a single line with []
.
3 3
1 2 3
4 5 6
7 8 9
12
1 2 3 6
1 4 7
</p>