-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathearthcrack.cpp
More file actions
79 lines (74 loc) · 1.66 KB
/
earthcrack.cpp
File metadata and controls
79 lines (74 loc) · 1.66 KB
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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
for(int i = 0 ; i < t ; i++){
int m, n, minnum = 1000000;
cin >> m >> n;
int firstlineminindex = n - 1;
vector<vector<int>> a;
for(int j = 0 ; j < m ; j++){
vector<int> b;
int q;
for(int k = 0 ; k < n ; k++){
cin >> q;
b.push_back(q);
if(j == 0 && q < minnum && k != 0 && k != n - 1){
firstlineminindex = k;
minnum = q;
cout << "\nminnum = " << minnum << " ";
cout << "\nminindex = " << firstlineminindex << " ";
}
}
a.push_back(b);
}
int sum = minnum,
minindex = firstlineminindex;
for(int j = 1 ; j < m ; j++){
int min = a[j][minindex];
if(minindex > 0 && minindex < m) {
int tempmin = min;
if(a[j][minindex - 1] < tempmin){
int left = minindex - 1;
min = a[j][left];
minindex = left;
}
if(a[j][minindex + 1] < min){
int right = minindex + 1;
min = a[j][minindex + 1];
minindex = right;
}
}
else if(minindex == 0){
if(a[j][minindex + 1] < min){
min = a[j][minindex + 1];
minindex++;
}
}
else if(minindex == n - 1){
if(a[j][minindex - 1] < min){
min = a[j][minindex - 1];
minindex--;
}
}
sum += min;
cout << "\nmin = " << min << " minindex = " << minindex << " sum = " << sum << " ";
}
cout << "\n" << sum << "\n";
}
return 0;
}
/*
2
8 5
1 6 7 2 3
7 9 5 2 1
1 1 8 8 6
6 5 6 3 9
9 1 4 8 6
4 2 9 7 7
2 3 2 5 8
5 8 2 4 4
*/