FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp/algorithms/dynamic-programming/gold_mine.cpp at master · AllAlgorithms/cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
cpp
Public archive
Notifications
You must be signed in to change notification settings
Fork
339
Star
841
Code
Issues
9
Pull requests
33
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp
/
algorithms
/
dynamic-programming
/
gold_mine.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (52 loc) · 1.9 KB
Breadcrumbs
cpp
/
algorithms
/
dynamic-programming
/
gold_mine.cpp
Copy path
File metadata and controls
61 lines (52 loc) · 1.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
C++ program to solve Gold Mine problem
#
include
<
bits/stdc++.h
>
using
namespace
std
;
const
int
MAX
=
100
;
//
Returns maximum amount of gold that can be collected
//
when journey started from first column and moves
//
allowed are right, right-up and right-down
int
getMaxGold
(
int
gold[][
MAX
],
int
m,
int
n)
{
//
Create a table for storing intermediate results
//
and initialize all cells to 0. The first row of
//
goldMineTable gives the maximum gold that the miner
//
can collect when starts that row
int
goldTable[m][n];
memset
(goldTable,
0
,
sizeof
(goldTable));
for
(
int
col=n-
1
; col>=
0
; col--)
{
for
(
int
row=
0
; row<m; row++)
{
//
Gold collected on going to the cell on the right(->)
int
right = (col==n-
1
)?
0
: goldTable[row][col+
1
];
//
Gold collected on going to the cell to right up (/)
int
right_up = (row==
0
|| col==n-
1
)?
0
:
goldTable[row-
1
][col+
1
];
//
Gold collected on going to the cell to right down (\)
int
right_down = (row==m-
1
|| col==n-
1
)?
0
:
goldTable[row+
1
][col+
1
];
//
Max gold collected from taking either of the
//
above 3 paths
goldTable[row][col] = gold[row][col] +
max
(right,
max
(right_up, right_down));
}
}
//
The max amount of gold collected will be the max
//
value in first column of all rows
int
res = goldTable[
0
][
0
];
for
(
int
i=
1
; i<m; i++)
res =
max
(res, goldTable[i][
0
]);
return
res;
}
//
Driver Code
int
main
()
{
int
gold[
MAX
][
MAX
]= { {
1
,
3
,
1
,
5
},
{
2
,
2
,
4
,
1
},
{
5
,
0
,
2
,
3
},
{
0
,
6
,
1
,
2
}
};
int
m =
4
, n =
4
;
cout <<
getMaxGold
(gold, m, n);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL