-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
76 lines (62 loc) · 1.12 KB
/
main.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
#include <bits/stdc++.h>
using namespace std;
int
calculate(vector<vector<int>> arr, int i, int j)
{
int sum=arr[i][j];
sum=sum+arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1];
sum=sum+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1];
return sum;
}
int
hourglassSum(vector<vector<int>> arr)
{
int res = -1000;
for (int i=1; i<5; i++)
{
for (int j=1; j<5; j++)
{
int sum = calculate(arr,i,j);
res = max(sum,res);
}
}
return res;
}
int
main(int argc, char **argv)
{
if (argc==1)
return 0;
ifstream filein (argv[1]);
string line;
string delimiter = " ";
vector<vector<int>> arr(6);
if (filein.is_open())
{
for (int i; i < 6; i++)
{
size_t pos = 0;
string token;
arr[i].resize(6);
int j=0;
getline ( filein, line );
string s = line;
while ((pos = s.find(delimiter)) != string::npos)
{
token = s.substr(0, pos);
s.erase(0, pos+delimiter.length());
arr[i][j]=stoi(token);
j++;
}
}
filein.close();
}
else
{
cout << "Unable to open file";
return 0;
}
int result = hourglassSum(arr);
cout << result << "\n";
return 0;
}