Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace JediGalaxy
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- class Program
- {
- static void Main(string[] args)
- {
- var dimensions = ParseInput(Console.ReadLine());
- var rows = dimensions[0];
- var cols = dimensions[1];
- var matrix = new int[rows][];
- FillMatrix(matrix,rows,cols);
- var input = Console.ReadLine();
- BigInteger collectedStarsSum = 0;
- var starsHittedByEvil = new Dictionary<int, HashSet<int>>();
- while (input != "Let the Force be with you")
- {
- var ivoCoordinates = ParseInput(input);
- var evilCoordinates = ParseInput(Console.ReadLine());
- CollectStars(matrix, ivoCoordinates, evilCoordinates, ref collectedStarsSum, starsHittedByEvil);
- input = Console.ReadLine();
- }
- Console.WriteLine(collectedStarsSum);
- }
- private static void CollectStars(int[][] matrix, int[] ivoCoordinates, int[] evilCoordinates, ref BigInteger collectedStarsSum, Dictionary<int, HashSet<int>> starsHittedByEvil)
- {
- var ivoRow = ivoCoordinates[0];
- var ivoCol = ivoCoordinates[1];
- var evilRow = evilCoordinates[0];
- var evilCol = evilCoordinates[1];
- HitStars(matrix, evilRow, evilCol, starsHittedByEvil);
- for (int r = ivoRow; r >= 0; r--)
- {
- if (ivoCol < 0 || r >= matrix.Length)
- {
- ivoCol++;
- continue;
- }
- if (ivoCol >= matrix[0].Length)
- {
- break;
- }
- if (!starsHittedByEvil.ContainsKey(r) || !starsHittedByEvil[r].Contains(ivoCol))
- {
- collectedStarsSum += matrix[r][ivoCol];
- }
- ivoCol++;
- }
- }
- private static void HitStars(int[][] matrix, int evilRow, int evilCol, Dictionary<int, HashSet<int>> starsHittedByEvil)
- {
- for (int r = evilRow; r >= 0; r--)
- {
- if (evilCol >= matrix[0].Length || (r >= matrix.Length))
- {
- evilCol--;
- continue;
- }
- if (evilCol < 0)
- {
- break;
- }
- if (!starsHittedByEvil.ContainsKey(r))
- {
- starsHittedByEvil.Add(r, new HashSet<int>());
- }
- starsHittedByEvil[r].Add(evilCol);
- evilCol--;
- }
- }
- private static void FillMatrix(int[][] matrix, int rows, int cols)
- {
- int num = 0;
- for (int r = 0; r < rows; r++)
- {
- matrix[r] = new int[cols];
- for (int c = 0; c < cols; c++)
- {
- matrix[r][c] = num++;
- }
- }
- }
- private static int[] ParseInput(string input)
- {
- return input
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement