日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

金矿问题

發布時間:2025/3/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 金矿问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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: 18Input: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:

因此,矩陣為:

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)可能移動到以下任一位置:

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-1DP[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)都按照以下方式進行更新。

So, for the earlier input matrix,

因此,對于較早的輸入矩陣,

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

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

After completion of second column (row wise computation),

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

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

現在找到最后一列的最大值,這就是可以收集的最大黃金量。

Result=19

結果= 19

C++ implementation:

C ++實現:

#include <bits/stdc++.h> using namespace std;int GoldMine(int** arr, int n, int m) {//DP tableint DP[n][m];memset(DP, 0, sizeof(DP));for (int i = 0; i < n; i++)DP[i][0] = arr[i][0];//for every columnfor (int j = 1; j < m; j++) {//check which option is better accordinglyfor (int i = 0; i < n; i++) {//choosing max of possible movesDP[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 columnint 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 arrayfor (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

總結

以上是生活随笔為你收集整理的金矿问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 污污网站免费 | 91高清无打码 | av免费在线不卡 | 黄黄视频在线观看 | 99热在线这里只有精品 | 午夜不卡av | 妺妺窝人体色777777 | 毛片日韩 | 99人妻少妇精品视频一区 | 黄色一级大片在线免费看国产一 | 免费av在线播放网址 | 国产激情影院 | www.啪啪 | 亚洲一区二区91 | a午夜| 五月婷婷中文字幕 | www.色婷婷.com | 无码内射中文字幕岛国片 | 日韩精品免费一区 | 中文字幕一区二区三区四区五区 | 久久不卡日韩美女 | 免费高清视频一区二区三区 | 真人毛片97级无遮挡精品 | 欧美老肥婆性猛交视频 | 午夜视频在线观看免费视频 | 91av在线免费视频 | 欧美资源在线观看 | 国产成人传媒 | 成人免费一区 | 亚洲av色香蕉一区二区三区 | 亚洲国产精品激情在线观看 | 亚洲www久久久 | 一级午夜 | 国产精品人妻 | 婷婷天堂| 7色av| 精品无码一区二区三区蜜臀 | 久久1024 | 波多野结衣视频网站 | 国产成人久久 | 日本性高潮视频 | 黄色xxxxx | 日韩精品电影网 | 国产情侣在线播放 | 日韩av在线不卡 | 神马三级我不卡 | 久久亚洲av成人无码国产电影 | 国产特级aaaaaa大片 | 伊人最新网址 | 中文字幕一区二区三区四区欧美 | 亚洲人成无码www久久久 | 成人自拍视频网 | 国产主播专区 | 一区二区三区伦理片 | 精品人妻伦一区二区三区久久 | 欧美精品18 | 色诱久久av | 在线亚洲自拍 | 91亚洲精品一区二区乱码 | 黄色天堂网站 | 青青操视频在线 | 日本精品一区二区三区在线观看 | 超碰极品 | 三级艳丽杨钰莹三级 | 国产精品99久久久久久动医院 | 中文在线亚洲 | 初高中福利视频网站 | 9i看片成人免费看片 | 日韩高清影视 | 亚洲一区二区三区色 | 少妇激情在线 | 日韩欧美在线视频播放 | 男人日女人在线观看 | 欧美日韩激情一区二区 | 亚州av在线 | 亚洲国产精品免费在线观看 | 黄色天堂 | 色噜噜狠狠一区二区三区果冻 | 久久综合高清 | 日韩久久综合 | 日韩久久电影 | 欧美视频亚洲视频 | 美女黄视频大全 | 国产原创在线观看 | h官场少妇第三部分 | 欧美 日韩 高清 | а√中文在线资源库 | 成人黄色短视频在线观看 | jav中文字幕 | 国产少妇在线 | 日本特黄特黄刺激大片 | 日韩欧美综合在线 | japanese24hdxxxx中文字幕 | 色爽爽爽爽爽爽爽爽 | 欧美人与按摩师xxxx | 亚洲一区二区精品在线观看 | 成人做爰www免费看视频网站 | 国产精品高潮呻吟久久av黑人 | 在线观看国产精品入口男同 |