-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1301.cpp
111 lines (91 loc) · 2.87 KB
/
1301.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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
/**
*
* i
* ^
* |
* |
* |
* |
* j <-------- 0
*/
class Solution {
public:
static inline int boardToInt(vector<string> &board, int i, int j) {
const auto &ch = board[i][j];
const int ret = ch == 'S' ? 0
: ch == 'E' ? 0
: ch == 'X' ? -10e5
: ch - '0';
// cout << i << "," << j << "=" << ret << endl;
return ret;
}
vector<int> pathsWithMaxScore(vector<string> &board) {
const int M = board.size();
const int N = board[0].size();
auto dpScore = vector<vector<int>>(M, vector<int>(N, 0));
auto dpPaths = vector<vector<int>>(M, vector<int>(N, 0));
for (auto i = 0; i < M; i++) {
for (auto j = 0; j < N; j++) {
if (i == 0 && j == 0) {
dpPaths[0][0] = 1;
continue;
}
int lm = -10e5;
int pa = 0;
if (j - 1 >= 0) {
lm = max(lm, boardToInt(board, M - 1 - i, N - 1 - j + 1) +
dpScore[i][j - 1]);
}
if (i - 1 >= 0) {
lm = max(lm, boardToInt(board, M - 1 - i + 1, N - 1 - j) +
dpScore[i - 1][j]);
}
if (i - 1 >= 0 && j - 1 >= 0) {
lm = max(lm, boardToInt(board, M - 1 - i + 1, N - 1 - j + 1) +
dpScore[i - 1][j - 1]);
}
dpScore[i][j] = lm >= 0 ? lm : -10e5;
// cout << "dp(" << i << "," << j << ") = " << dpScore[i][j] << endl;
if (dpScore[i][j] >= 0) {
if (j - 1 >= 0) {
if (dpScore[i][j] == boardToInt(board, M - 1 - i, N - 1 - j + 1) +
dpScore[i][j - 1]) {
pa = (pa + dpPaths[i][j - 1]) % (1000000000 + 7);
}
}
if (i - 1 >= 0) {
if (dpScore[i][j] == boardToInt(board, M - 1 - i + 1, N - 1 - j) +
dpScore[i - 1][j]) {
pa = (pa + dpPaths[i - 1][j]) % (1000000000 + 7);
}
}
if (i - 1 >= 0 && j - 1 >= 0) {
if (dpScore[i][j] ==
boardToInt(board, M - 1 - i + 1, N - 1 - j + 1) +
dpScore[i - 1][j - 1]) {
pa = (pa + dpPaths[i - 1][j - 1]) % (1000000000 + 7);
}
}
}
dpPaths[i][j] = pa;
}
}
return {dpScore[M - 1][N - 1] > 0 ? dpScore[M - 1][N - 1] : 0,
dpPaths[M - 1][N - 1]};
}
};
int main(int argc, char const *argv[]) {
auto sol = new Solution();
vector<string> testCases[] = {{"E23", "2X2", "12S"}, {"EX", "XS"}};
for (auto t : testCases) {
auto ret = sol->pathsWithMaxScore(t);
cout << ret[0] << "," << ret[1] << endl;
}
delete sol;
return 0;
}