-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path116.cpp
113 lines (93 loc) · 2.28 KB
/
116.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
#include <iostream>
#include <cstdint>
#include <algorithm>
#include <vector>
using namespace std;
void solve();
void calc();
int64_t numRows;
int64_t numCols;
int64_t input[21][111];
int64_t sum[21][111];
int64_t path[111];
int64_t output;
int64_t up;
int64_t center;
int64_t down;
pair<int64_t, int64_t> upPair;
pair<int64_t, int64_t> centerPair;
pair<int64_t, int64_t> downPair;
int main() {
while (cin >> numRows >> numCols) {
if ((numRows != 0) && (numCols != 0)) {
solve();
}
}
return 0;
}
void solve() {
for (int64_t i = 0; i < 21; i++) {
fill(input[i], input[i] + 111, 0);
fill(sum[i], sum[i] + 111, 0);
}
fill(path, path + 111, 0);
for (int64_t i = 0; i < numRows; i++) {
for (int64_t j = 0; j < numCols; j++) {
cin >> input[i][j];
}
}
for (int64_t i = 0; i < numRows; i++) {
sum[i][(numCols - 1)] = input[i][(numCols - 1)];
}
calc();
for (int64_t i = 0; i < numCols; i++) {
if (i) {
cout << " ";
}
cout << (path[i] + 1);
}
cout << endl << output << endl;
}
void calc() {
for (int64_t j = numCols - 2; j >= 0; j--) {
for (int64_t i = 0; i < numRows; i++) {
up = sum[(i + numRows - 1) % numRows][j + 1];
center = sum[i][j + 1];
down = sum[(i + numRows + 1) % numRows][j + 1];
sum[i][j] = min(up, min(center, down)) + input[i][j];
}
}
output = sum[0][0];
for (int64_t i = 1; i < numRows; i++) {
if (sum[i][0] < output) {
output = sum[i][0];
path[0] = i;
}
}
for (int64_t j = 1; j < numCols; j++) {
upPair.first = sum[(path[j - 1] + numRows - 1) % numRows][j];
upPair.second = ((path[j - 1] + numRows - 1) % numRows);
centerPair.first = sum[path[j - 1]][j];
centerPair.second = (path[j - 1]);
downPair.first = sum[(path[j - 1] + numRows + 1) % numRows][j];
downPair.second = ((path[j - 1] + numRows + 1) % numRows);
int64_t tempMin = min(upPair.first, min(centerPair.first, downPair.first));
vector<pair<int64_t, int64_t> > lexo;
if (tempMin == upPair.first) {
lexo.push_back(upPair);
}
if (tempMin == centerPair.first) {
lexo.push_back(centerPair);
}
if (tempMin == downPair.first) {
lexo.push_back(downPair);
}
int64_t lexoMin = lexo.at(0).second;
for (int64_t k = 1; k < lexo.size(); k++) {
if (lexo[k].second < lexoMin) {
lexoMin = lexo[k].second;
}
}
path[j] = lexoMin;
}
}