We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// stores the sums, in orderstd::set<int>hourglass_sums;/*A core is the middle point of the glasshoura b c d e f g in the above, the core is d */// finds the sum of the three neighbors above the coreintsumOfUpperNeighbors(intcore_y,intcore_x,vector<vector<int>>arr){intleft_most_cordinate=core_x-1;intright_most_cordinate=core_x+1;intupper_coordinate=core_y-1;intsum=0;sum+=arr[upper_coordinate][left_most_cordinate];sum+=arr[upper_coordinate][core_x];sum+=arr[upper_coordinate][right_most_cordinate];returnsum;}// finds the sum of the three neighbors above the coreintsumOfLowerNeighbors(intcore_y,intcore_x,vector<vector<int>>arr){intleft_most_cordinate=core_x-1;intright_most_cordinate=core_x+1;intlower_coordinate=core_y+1;intsum=0;sum+=arr[lower_coordinate][left_most_cordinate];sum+=arr[lower_coordinate][core_x];sum+=arr[lower_coordinate][right_most_cordinate];returnsum;}inthourglassSum(vector<vector<int>>arr){inttotal_sum=0;for(inti=1;i<5;i++){for(intj=1;j<5;j++){total_sum+=arr[i][j];total_sum+=sumOfUpperNeighbors(i,j,arr);total_sum+=sumOfLowerNeighbors(i,j,arr);hourglass_sums.insert(total_sum);total_sum=0;
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
C++
}