📌  相关文章
📜  找到到达矩阵末尾所需的最小步骤 |套装 – 1

📅  最后修改于: 2021-09-17 16:08:03             🧑  作者: Mango

给定一个由正整数组成的 2d 矩阵,任务是找到到达矩阵末尾(最左边的底部单元格)所需的最小步数。如果我们在单元格 (i, j) 处,我们可以转到单元格 (i, j+arr[i][j]) 或 (i+arr[i][j], j)。我们不能越界。如果不存在路径,则打印 -1。
例子:

Input : 
mat[][] = {{2, 1, 2},
           {1, 1, 1},
           {1, 1, 1}}
Output : 2
Explanation : The path will be {0, 0} -> {0, 2} -> {2, 2}
Thus, we are reaching end in two steps.

Input :
mat[][] = {{1, 1, 2},
           {1, 1, 1},
           {2, 1, 1}}
Output : 3

一个简单的解决方案是探索所有可能的解决方案。这将需要指数级的时间。
更好的方法:我们可以使用动态规划在多项式时间内解决这个问题。
让我们决定“dp”的状态。我们将在 2d DP 上构建我们的解决方案。
假设我们在单元格 {i, j}。我们将尝试找到从该单元格到达单元格 (n-1, n-1) 所需的最少步数。
我们只有两种可能的路径,即到单元格 {i, j+arr[i][j]} 或 {i+arr[i][j], j}。
一个简单的递推关系是:

dp[i][j] = 1 + min(dp[i+arr[i]][j], dp[i][j+arr[i][j]])

下面是上述想法的实现:

C++
// C++ program to impplement above approach
 
#include 
#define n 3
using namespace std;
 
// 2d array to store
// states of dp
int dp[n][n];
 
// array to determine whether
// a state has been solved before
int v[n][n];
 
// Function to find the minimum number of
// steps to reach the end of matrix
int minSteps(int i, int j, int arr[][n])
{
    // base cases
    if (i == n - 1 and j == n - 1)
        return 0;
 
    if (i > n - 1 || j > n - 1)
        return 9999999;
 
    // if a state has been solved before
    // it won't be evaluated again.
    if (v[i][j])
        return dp[i][j];
 
    v[i][j] = 1;
 
    // recurrence relation
    dp[i][j] = 1 + min(minSteps(i + arr[i][j], j, arr),
                       minSteps(i, j + arr[i][j], arr));
 
    return dp[i][j];
}
 
// Driver Code
int main()
{
    int arr[n][n] = { { 2, 1, 2 },
                      { 1, 1, 1 },
                      { 1, 1, 1 } };
 
    int ans = minSteps(0, 0, arr);
    if (ans >= 9999999)
        cout << -1;
    else
        cout << ans;
 
    return 0;
}


Java
// Java program to impplement above approach
class GFG {
 
    static int n = 3;
 
    // 2d array to store
    // states of dp
    static int dp[][] = new int[n][n];
 
    // array to determine whether
    // a state has been solved before
    static int[][] v = new int[n][n];
 
    // Function to find the minimum number of
    // steps to reach the end of matrix
    static int minSteps(int i, int j, int arr[][])
    {
        // base cases
        if (i == n - 1 && j == n - 1) {
            return 0;
        }
 
        if (i > n - 1 || j > n - 1) {
            return 9999999;
        }
 
        // if a state has been solved before
        // it won't be evaluated again.
        if (v[i][j] == 1) {
            return dp[i][j];
        }
 
        v[i][j] = 1;
 
        // recurrence relation
        dp[i][j] = 1 + Math.min(minSteps(i + arr[i][j], j, arr), minSteps(i, j + arr[i][j], arr));
 
        return dp[i][j];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[][] = { { 2, 1, 2 },
                        { 1, 1, 1 },
                        { 1, 1, 1 } };
 
        int ans = minSteps(0, 0, arr);
        if (ans >= 9999999) {
            System.out.println(-1);
        }
        else {
            System.out.println(ans);
        }
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 program to implement above approach
import numpy as np;
 
n = 3
 
# 2d array to store
# states of dp
dp = np.zeros((n, n));
 
# array to determine whether
# a state has been solved before
v = np.zeros((n, n));
 
# Function to find the minimum number of
# steps to reach the end of matrix
def minSteps(i, j, arr) :
 
    # base cases
    if (i == n - 1 and j == n - 1) :
        return 0;
 
    if (i > n - 1 or j > n - 1) :
        return 9999999;
 
    # if a state has been solved before
    # it won't be evaluated again.
    if (v[i][j]) :
        return dp[i][j];
 
    v[i][j] = 1;
 
    # recurrence relation
    dp[i][j] = 1 + min(minSteps(i + arr[i][j], j, arr),
                    minSteps(i, j + arr[i][j], arr));
 
    return dp[i][j];
 
 
# Driver Code
arr = [ [ 2, 1, 2 ],
            [ 1, 1, 1 ],
            [ 1, 1, 1 ]
            ];
             
ans = minSteps(0, 0, arr);
if (ans >= 9999999) :
    print(-1);
         
else :
    print(ans);
 
# This code is contributed by AnkitRai01


C#
// C# program to impplement above approach
using System;
 
class GFG
{
     
    static int n = 3;
 
    // 2d array to store
    // states of dp
    static int [,]dp = new int[n, n];
 
    // array to determine whether
    // a state has been solved before
    static int[,] v = new int[n, n];
 
    // Function to find the minimum number of
    // steps to reach the end of matrix
    static int minSteps(int i, int j, int [,]arr)
    {
        // base cases
        if (i == n - 1 && j == n - 1)
        {
            return 0;
        }
 
        if (i > n - 1 || j > n - 1)
        {
            return 9999999;
        }
 
        // if a state has been solved before
        // it won't be evaluated again.
        if (v[i, j] == 1)
        {
            return dp[i, j];
        }
 
        v[i, j] = 1;
 
        // recurrence relation
        dp[i, j] = 1 + Math.Min(minSteps(i + arr[i,j], j, arr),
                            minSteps(i, j + arr[i,j], arr));
 
        return dp[i, j];
    }
 
    // Driver Code
    static public void Main ()
    {
        int [,]arr = { { 2, 1, 2 },
                        { 1, 1, 1 },
                        { 1, 1, 1 } };
 
        int ans = minSteps(0, 0, arr);
        if (ans >= 9999999)
        {
            Console.WriteLine(-1);
        }
        else
        {
            Console.WriteLine(ans);
        }
    }
}
 
// This code contributed by ajit.


Javascript


输出:

2

时间复杂度:O(N 2 )。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程