-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad9_ms.cpp
127 lines (110 loc) · 2.88 KB
/
zad9_ms.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
using namespace std;
template <typename T>
T **make_2d(int x, int y, T defaultVal);
template <typename T>
void free_2d(T **dp, int y);
template <typename T>
void print(T **dp, int x, int y, void (*printT)(T));
void print_number(int n);
int find_max_passengers_on_junction(int y, int x, int **dp)
{
int junctionToTheRight = dp[y][x + 1];
int juntionBelow = dp[y + 1][x];
return max(junctionToTheRight, juntionBelow);
}
// T(n) = O(n^2)
// M(n) = O(n^2)
int autobus(int **passengers, int n, int m)
{
int **dp = make_2d<int>(n, m, 0);
const int northStreet = n - 1;
const int eastStreet = m - 1;
dp[eastStreet][northStreet] = passengers[eastStreet][northStreet];
// fill north edge
for (int i = eastStreet - 1; i >= 0; i--)
{
dp[i][northStreet] = dp[i + 1][northStreet] + passengers[i][northStreet];
}
// fill east edge
for (int i = northStreet - 1; i >= 0; i--)
{
dp[eastStreet][i] = dp[eastStreet][i + 1] + passengers[eastStreet][i];
}
// fill rest of junctions \/
// distance between the street on north / east edge
for (int d = 1; d <= n - 1; d++)
{
// iterator from nort/east corner od distance d to the furtherest on given street
for (int i = n - 1 - d; i >= 0; i--)
{
// north street walker
if (northStreet - d >= 0)
dp[i][northStreet - d] = passengers[i][northStreet - d] + find_max_passengers_on_junction(i, northStreet - d, dp);
// east street walker
if (eastStreet - d >= 0)
dp[eastStreet - d][i] = passengers[eastStreet - d][i] + find_max_passengers_on_junction(eastStreet - d, i, dp);
}
}
// cout<<endl;
// print(dp,n,m, print_number);
int results = dp[0][0];
free_2d(dp, n);
return results;
}
int main()
{
int n,m;
cin >> n>>m;
int **passengers = make_2d<int>(n, m, 0);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> passengers[i][j];
}
}
int results = autobus(passengers, n,m);
cout << results << endl;
free_2d(passengers, m);
return 0;
}
template <typename T>
T **make_2d(int x, int y, T defaultVal)
{
T **ret = (T **)malloc((size_t)y * (sizeof(T *)));
for (int i = 0; i < y; i++)
{
ret[i] = (T *)malloc((size_t)x * sizeof(T));
for (int j = 0; j < x; j++)
{
ret[i][j] = defaultVal;
}
}
return ret;
}
template <typename T>
void free_2d(T **dp, int y)
{
for (int i = 0; i < y; i++)
{
free(dp[i]);
}
free(dp);
}
template <typename T>
void print(T **dp, int x, int y, void (*printT)(T))
{
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
printT(dp[i][j]);
}
cout << endl;
}
}
void print_number(int n)
{
cout << n << " ";
}