#C9529. Exact Item Collection
Exact Item Collection
Exact Item Collection
You are given a grid with n rows and m columns. Each cell of the grid contains a number representing the number of items at that cell. Starting from the top-left cell (1, 1), you are allowed to move only to the right or downward. Your goal is to determine if there exists a path from the starting cell to the bottom-right cell (n, m) such that the sum of the items collected along the path is exactly k.
Formally, if you denote an allowed move by either right (from (i, j) to (i, j+1)) or down (from (i, j) to (i+1, j)), then let \(S\) be the sum of values on the cells encountered on the path (including the start and end cells). You need to check whether \(S = k\) for some valid path.
If such a path exists, output YES
; otherwise output NO
.
inputFormat
The first line contains an integer \(T\), the number of test cases. For each test case:
- The first line contains three integers \(n\), \(m\), and \(k\) representing the number of rows, the number of columns, and the required exact total items respectively.
- Then follow \(n\) lines, each containing \(m\) integers, where the \(j^{th}\) integer in the \(i^{th}\) line denotes the number of items at cell \((i, j)\).
Input is read from standard input.
outputFormat
For each test case, output one line containing YES
if it is possible to collect exactly \(k\) items along some valid path from the top-left to the bottom-right cell, or NO
otherwise. Output is written to standard output.
3
2 2 3
1 2
1 0
3 3 6
0 2 1
1 0 2
1 2 1
3 5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
YES
YES
NO
</p>