Advertisement
BSO90

Zig Zag

Jul 4th, 2021
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Zig_Zag
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int[] size = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int n = size[0];
  12.             int m = size[1];
  13.             int[,] matrix = new int[n, m];
  14.             int initialNum = 1;
  15.             int startRow = 0;
  16.             int currRow = 0;
  17.             int currCol = 0;
  18.             ulong sumPath = 0;
  19.             FindMatrixSumFollowingZigZagPath(n, m, matrix, initialNum, currRow, currCol, startRow, sumPath);
  20.  
  21.         }
  22.  
  23.         private static void FindMatrixSumFollowingZigZagPath(int n, int m, int[,] matrix, int initialNum, int startRow, int currRow, int currCol, ulong sumPath)
  24.         {
  25.             initialNum = FillMatrix(n, m, matrix, initialNum, startRow);
  26.             for (int row = 0; row < n; row += 2)
  27.             {
  28.                 for (int col = 0; col < m; col += 2)
  29.                 {
  30.                     if (row == 0 || row == n - 1 || col == 0 || col == m - 1)
  31.                     {
  32.                         sumPath += (ulong)matrix[row, col];
  33.                     }
  34.                     else
  35.                     {
  36.                         sumPath += (ulong)(matrix[row, col] * 2);
  37.                     }
  38.                 }
  39.             }
  40.             for (int row = 1; row < n; row += 2)
  41.             {
  42.  
  43.                 for (int col = 1; col < m; col += 2)
  44.                 {
  45.                     if (col == 0 || col == m - 1)
  46.                     {
  47.                         sumPath += (ulong)matrix[row, col];
  48.                     }
  49.                     else
  50.                     {
  51.                         sumPath += (ulong)(matrix[row, col] * 2);
  52.                     }
  53.                    
  54.                 }
  55.             }
  56.  
  57.             Console.WriteLine(sumPath);
  58.         }
  59.  
  60.         private static int FillMatrix(int n, int m, int[,] matrix, int initialNum, int startRow)
  61.         {
  62.             for (int row = 0; row < n; row++)
  63.             {
  64.                 for (int col = 0; col < m; col++)
  65.                 {
  66.                     matrix[row, col] = initialNum;
  67.                     initialNum += 3;
  68.                 }
  69.                 initialNum = matrix[row, startRow + 1];
  70.             }
  71.  
  72.             return initialNum;
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement