金矿问题

Description:

描述:

This is a standard interview problem featured in interview coding rounds of Amazon, Flipkart.

这是亚马逊Flipkart的采访编码回合中的标准采访问题。

Problem statement:

问题陈述:

Given a gold mine of n*m dimensions, each cell in this mine contains a positive integer which is the amount of gold in tons. Initially, the miner is at the first column but can be at any row. He can move only right or right up or right down from the current cell, i.e., the miner can move to the cell diagonally up towards the right or right or diagonally down towards the right. Find out the maximum amount of gold he can collect.

给定一个 n * m尺寸 的金矿,该矿中的每个单元格都包含一个正整数,该黄金数量以吨为单位。 最初,该矿工位于第一列,但可以位于任何行。 他只能从当前牢房向右或向右或向下移动,即,矿工可以向右或向右斜向上或向右斜向向下移动到该牢房。 找出他可以收集的最大数量的黄金。

    Input:
 
    Matrix:
     {{1,5,12},
       {2,4,4},
       {0,6,4}
       {3,0,0}	
      }
    Output: 
    18

    Input:
    Matrix:
     {{1,3,1,5},
       {2,2,4,1},
       {5,0,2,3},
       {0,6,11,2}
     }
    Output: 
    25

Explanation with example

举例说明

So, the matrix is:

因此,矩阵为:

Gold Mine Problem (1)

So, the miner starts from the first column (any row) and he has to reach the last row with maximum gold.

因此,矿工从第一列(任意行)开始,他必须以最大的金数到达最后一行。

To make a note, the possible moves from a cell (i,j) is to either of,

要说明一下,从单元格(i,j)可能移动到以下任一位置:

Gold Mine Problem (i)

Now, it seems apparently that greedy may work for the problem that is at the first column pick the cell with maximum value and then move to next best neighbouring cell.

现在,似乎贪婪似乎可以解决以下问题:在第一列中选​​择具有最大值的单元格,然后移至下一个最佳相邻单元格。

If we follow the above greedy approach,

如果我们遵循以上贪婪的方法,

We would pick (3,0) as our starting cell since that contains maximum gold out of the first column. The next best move would be (2,1) and then to (2,2).

我们将(3,0)作为起始单元格,因为它包含第一列中最多的金。 下一个最佳移动是(2,1)然后到(2,2)

So, the total gold collected is = (3+6+4) = 13

因此,收集的总金= (3 + 6 + 4)= 13

Is this the global maximum? Do our local maximum choices lead to global maximum?

这是全球最大值吗? 我们的局部最大选择会导致整体最大吗?

No, it's not the global maximum.

不,这不是全局最大值。

The global maximum path would be: (1,0) ->(0,1)->(0,2)

全局最大路径为: (1,0)->(0,1)->(0,2)

Total coin that can be achieved: (2+5+12) = 19

可以达到的总硬币数: (2 + 5 + 12)= 19

So, the local best choices don't lead to the global best and hence we need dynamic programming.

因此,本地的最佳选择不会导致全球最佳,因此我们需要动态编程。

Solution Approach:

解决方法:

Create a DP matrix of dimension m*n: DP[m][n]

创建尺寸为m * n的DP矩阵: DP [m] [n]

The first column of the DP matrix would be same as the input matrix. Rest of the columns are 0,

DP矩阵的第一列将与输入矩阵相同。 其余列为0,

    for i=0 to n-1
        DP[i][0]=arr[i][0];

For the other columns, update DP value for every row. For every cell (i,j) update like following way.

对于其他列,更新每行的DP值。 对于每个单元格(i,j)都按照以下方式进行更新。

Gold Mine Problem (ii)

So, for the earlier input matrix,

因此,对于较早的输入矩阵,

Gold Mine Problem (2)

After completion of the First column (row wise computing),

完成第一列(行计算)后,

Gold Mine Problem (3)

After completion of second column (row wise computation),

完成第二列后(行计算),

Gold Mine Problem (4)

Now find the maximum of the final column, that's the maximum gold that can be collected.

现在找到最后一列的最大值,这就是可以收集的最大黄金量。

Gold Mine Problem (5)

Result=19

结果= 19

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int GoldMine(int** arr, int n, int m)
{
    //DP table
    int DP[n][m];

    memset(DP, 0, sizeof(DP));

    for (int i = 0; i < n; i++)
        DP[i][0] = arr[i][0];

    //for every column
    for (int j = 1; j < m; j++) {
        //check which option is better accordingly
        for (int i = 0; i < n; i++) {
            //choosing max of possible moves
            DP[i][j] = arr[i][j];
            int val = DP[i][j - 1];
            if (i - 1 >= 0) {
                if (val < DP[i - 1][j - 1])
                    val = DP[i - 1][j - 1];
            }
            if (i + 1 < n) {
                if (val < DP[i + 1][j - 1])
                    val = DP[i + 1][j - 1];
            }
            DP[i][j] += val;
        }
    }
    // find the maximum of the last column
    int gold = DP[0][m - 1];
    for (int i = 1; i < n; i++) {
        if (DP[i][m - 1] > gold)
            gold = DP[i][m - 1];
    }

    return gold;
}

int main()
{
    int n, item, m;

    cout << "Enter matrix dimensions, m & n\n";
    cin >> n >> m;

    cout << "Input matrix cells\n";
    int** arr = (int**)(malloc(sizeof(int*) * n));

    //input array
    for (int j = 0; j < n; j++) {
        arr[j] = (int*)(malloc(sizeof(int) * m));
        for (int k = 0; k < m; k++)
            cin >> arr[j][k];
    }

    cout << "Max amount of gold that can be collected: " << GoldMine(arr, n, m) << endl;

    return 0;
}

Output

输出量

Enter matrix dimensions, m & n
4 3
Input matrix cells
1 5 12
2 4 4
0 6 4
3 0 0
Max amount of gold that can be collected: 19


翻译自: https://www.includehelp.com/icp/gold-mine-problem.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值