Advertisement
alpenglow

ElemBeamsSum

Apr 6th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. class Program
  2.     {
  3.         static int ElemBeamsSum(int[,] arr, int x, int y, int len)
  4.         {
  5.             int i1 = x - y >= 0 ? x - y : 0,
  6.                 j1 = i1 == 0 ? y - x : 0,
  7.                 i1_stop = x >= y ? len - 1 : (len - 1) - y + x,
  8.                 j1_stop = x <= y ? len - 1 : (len - 1) - x + y,
  9.                 i2 = x + y >= len - 1 ? len - 1 : x + y,
  10.                 j2 = x + y <= len - 1 ? 0 : x + y - (len - 1),
  11.                 i2_stop = j2,
  12.                 j2_stop = i2,
  13.                 sum = 0;
  14.             while (i1 <= i1_stop && j1 <= j1_stop || i2 >= i2_stop && j2 <= j2_stop)
  15.             {
  16.                 if (i1 <= i1_stop && j1 <= j1_stop)
  17.                 {
  18.                     sum += arr[i1, j1];
  19.                     i1++; j1++;
  20.                 }
  21.                 if (i2 >= i2_stop && j2 <= j2_stop)
  22.                 {
  23.                     sum += arr[i2, j2];
  24.                     i2--;
  25.                     j2++;
  26.                 }
  27.             }
  28.             return x == 0 || y == 0 || x == len - 1 || y == len - 1 ? sum - arr[x, y] : sum;
  29.  
  30.         }
  31.         static void Main(string[] args)
  32.         {
  33.             int[,] arr = {
  34.             { 1, 1, 2, 3, 4 },
  35.             { 1, 1, 1, 1, 1 },
  36.             { 2, 1, 2, 1, 2 },
  37.             { 3, 1, 1, 3, 3 },
  38.             { 4, 1, 2, 3, 4 },
  39.             };
  40.             Console.WriteLine(ElemBeamsSum(arr, 0, 1, 5));
  41.         }
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement