给定一个维度为 m X n 的成本矩阵。问题是找到从左上角到矩阵中某个单元格的最小路径。路径的总成本是路径中访问的所有单元格的成本总和. 只允许 2 次移动:要么向下移动一行,要么向右移动一列。任何时候你都不能离开矩阵此外,一些细胞被标记为障碍物,不能被踩到。需要回答以下形式的几个查询: tx ty k。此查询的输出应该是从左上角到在 tx 行约束中的 ty 列索引的单元格的路径的第 k 个最小成本:
1<= m,n <= 100 0 <= tx < m 0 <= ty <n 1 <= k <= 101
The obstacles in matrix are marked by "##" (quotes only for clarity)
The values in every cell that is not an obstacle, varies from -9 to 99 only.
tx and ty are both 0-indexed.
If k exceeds the total paths to cell (tx, ty) output "Not so many paths"
If (tx, ty) is an obstacle, output "Obstacle".
There will never be an obstacle on the top-left corner or bottom-right corner.
Input:
The first line contains the number of test cases T,
The first line of each test case contains two space-separated integers m and n.
The next m lines each contain n integers, denoting the matrix.
The next line contains an integer q, denoting the number of queries to come.
The next q lines each contain 3 space separated integers, tx ty and k
Output:
For each query output the kth shortest path
我尝试的是导致 TLE 的回溯。(时间限制为 1 秒)。每当我到达目标单元格时,我将路径成本存储在向量中。最后在对向量进行排序后打印向量中的第 K 个值。 .但我需要更有效的方法来解决问题..可以在这里使用动态编程..???