-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut158.c++
66 lines (56 loc) · 1.88 KB
/
tut158.c++
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
62
63
64
65
66
/*Gold Mine Problem -> Given a gold mine of n*m dimensions. Each field in this mine contains a positive integer which is the amount of gold in tons. Initially the miner is at first column but can be at any row. He can move only (right,right up or right down) that is from a given cell, the miner can move to the cell diagonally up towards the right or right or diagonally down towards the right. Find out maximum amount of gold he can collect.
Examples:
Input : mat[][] = {{1, 3, 3},
{2, 1, 4},
{0, 6, 4}};
Output : 12
{(1,0)->(2,1)->(2,2)}
Input: mat[][] = { {1, 3, 1, 5},
{2, 2, 4, 1},
{5, 0, 2, 3},
{0, 6, 1, 2}};
Output : 16
(2,0) -> (1,1) -> (1,2) -> (0,3) OR (2,0) -> (3,1) -> (2,2) -> (2,3)
Input : mat[][] = {{10, 33, 13, 15},
{22, 21, 04, 1},
{5, 0, 2, 3},
{0, 6, 14, 2}};
Output : 83
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int row = 3, column = 3;
int mat[row][column] = {{1, 3, 3},
{2, 1, 4},
{0, 6, 4}};
for (int j = 1; j < column; j++)
{
for (int i = 0; i < row; i++)
{
// all 3 paths possible
if (i > 0 && i < row - 1)
{
mat[i][j] += max({mat[i][j - 1], mat[i - 1][j - 1], mat[i + 1][j - 1]});
}
// right down not possible
else if (i == 0 && i < row - 1)
{
mat[i][j] += max(mat[i][j - 1], mat[i + 1][j - 1]);
}
// right up not possible
else
{
mat[i][j] += max(mat[i][j - 1], mat[i - 1][j - 1]);
}
}
}
int ans = 0;
for (int i = 0; i < row; i++)
{
ans = max(mat[i][column - 1], ans);
}
cout << ans;
return 0;
}