Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Zig_Zag
- {
- class Program
- {
- static void Main()
- {
- int[] size = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int n = size[0];
- int m = size[1];
- int[,] matrix = new int[n, m];
- int initialNum = 1;
- int startRow = 0;
- int currRow = 0;
- int currCol = 0;
- ulong sumPath = 0;
- FindMatrixSumFollowingZigZagPath(n, m, matrix, initialNum, currRow, currCol, startRow, sumPath);
- }
- private static void FindMatrixSumFollowingZigZagPath(int n, int m, int[,] matrix, int initialNum, int startRow, int currRow, int currCol, ulong sumPath)
- {
- initialNum = FillMatrix(n, m, matrix, initialNum, startRow);
- for (int row = 0; row < n; row += 2)
- {
- for (int col = 0; col < m; col += 2)
- {
- if (row == 0 || row == n - 1 || col == 0 || col == m - 1)
- {
- sumPath += (ulong)matrix[row, col];
- }
- else
- {
- sumPath += (ulong)(matrix[row, col] * 2);
- }
- }
- }
- for (int row = 1; row < n; row += 2)
- {
- for (int col = 1; col < m; col += 2)
- {
- if (col == 0 || col == m - 1)
- {
- sumPath += (ulong)matrix[row, col];
- }
- else
- {
- sumPath += (ulong)(matrix[row, col] * 2);
- }
- }
- }
- Console.WriteLine(sumPath);
- }
- private static int FillMatrix(int n, int m, int[,] matrix, int initialNum, int startRow)
- {
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < m; col++)
- {
- matrix[row, col] = initialNum;
- initialNum += 3;
- }
- initialNum = matrix[row, startRow + 1];
- }
- return initialNum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement