Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Numerics;
- namespace Navigation
- {
- class Program
- {
- static void Main(string[] args)
- {
- int r = int.Parse(Console.ReadLine());
- int c = int.Parse(Console.ReadLine());
- int n = int.Parse(Console.ReadLine());
- int[] codes = Console.ReadLine().Split().Select(int.Parse).ToArray();
- BigInteger[,] matrix = new BigInteger[r, c];
- InitializeMatrix(matrix, r, c);
- //PrintMatrix(matrix, r, c);
- int coeff = Math.Max(r, c);
- BigInteger sum = 0;
- int counter = 0;
- int[] currentIndex = new int[2] { r -1, 0};
- while (counter <= n - 1)
- {
- sum += Move(sum, matrix, codes, coeff, r, c, counter, currentIndex);
- counter++;
- }
- Console.WriteLine(sum);
- }
- static int[] RowCol (int coeff, int[] codes, int counter)
- {
- int[] coordinates = new int[2];
- coordinates[0] = codes[counter] / coeff;
- coordinates[1] = codes[counter] % coeff;
- return coordinates;
- }
- static BigInteger Right(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
- {
- BigInteger sum = 0;
- while (GoalRowCol[1] >= currentIndex[1])
- {
- sum += matrix[currentIndex[0], currentIndex[1]];
- matrix[currentIndex[0], currentIndex[1]] = 0;
- currentIndex[1]++;
- }
- return sum;
- }
- static BigInteger Left(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
- {
- BigInteger sum = 0;
- while (GoalRowCol[1] <= currentIndex[1])
- {
- sum += matrix[currentIndex[0], currentIndex[1]];
- matrix[currentIndex[0], currentIndex[1]] = 0;
- currentIndex[1]--;
- }
- return sum;
- }
- static BigInteger Up(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
- {
- BigInteger sum = 0;
- while (GoalRowCol[0] <= currentIndex[0])
- {
- sum += matrix[currentIndex[0], currentIndex[1]];
- matrix[currentIndex[0], currentIndex[1]] = 0;
- currentIndex[0]--;
- }
- return sum;
- }
- static BigInteger Down(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
- {
- BigInteger sum = 0;
- while (GoalRowCol[0] >= currentIndex[0])
- {
- sum += matrix[currentIndex[0], currentIndex[1]];
- matrix[currentIndex[0], currentIndex[1]] = 0;
- currentIndex[0]++;
- }
- return sum;
- }
- static BigInteger Move(BigInteger sum, BigInteger[,] matrix, int[] codes, int coeff, int r, int c, int counter, int[] currentIndex)
- {
- int[] GoalRowCol = RowCol(coeff, codes, counter);
- sum = 0;
- if (GoalRowCol[1] > currentIndex[1])
- {
- sum += Right(matrix, GoalRowCol, currentIndex);
- currentIndex[1] = GoalRowCol[1];
- }
- else if (GoalRowCol[1] < currentIndex[1])
- {
- sum += Left(matrix, GoalRowCol, currentIndex);
- currentIndex[1] = GoalRowCol[1];
- }
- if (GoalRowCol[1] == currentIndex[1])
- {
- if (GoalRowCol[0] < currentIndex[0])
- {
- sum += Up(matrix, GoalRowCol, currentIndex);
- currentIndex[0] = GoalRowCol[0];
- }
- else
- {
- sum += Down(matrix, GoalRowCol, currentIndex);
- currentIndex[0] = GoalRowCol[0];
- }
- }
- return sum;
- }
- //static void PrintMatrix(BigInteger[,] matrix, int r, int c)
- //{
- // for (int i = 0; i < r; i++)
- // {
- // for (int j = 0; j < c; j++)
- // {
- // Console.Write("{0, 4}", matrix[i, j]);
- // }
- // Console.WriteLine();
- // }
- //}
- static void InitializeMatrix(BigInteger[,] matrix, int r, int c)
- {
- int a = 0;
- for (int row = r - 1; row >= 0; row--)
- {
- for (int col = 0; col < c; col++)
- {
- matrix[row, col] = BigInteger.Pow(2, a + col);
- }
- a++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement