Advertisement
braveheart1989

BunkerBuster

Apr 30th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _01.BunkerBuster
  8. {
  9.    
  10.     class BunkerBuster
  11.     {
  12.         static long counter;
  13.  
  14.         static long[,] ReadMatrix(long rows, long cols, long[,] matrix)
  15.         {
  16.             for (int row = 0; row < rows; row++)
  17.             {
  18.                 long[] cells = Console.ReadLine()
  19.                     .Split(new char[] { ' ' }, StringSplitOptions
  20.                     .RemoveEmptyEntries)
  21.                     .Select(long.Parse)
  22.                     .ToArray();
  23.                 for (int col = 0; col < cols; col++)
  24.                 {
  25.                     matrix[row, col] = cells[col];
  26.                 }
  27.             }
  28.             return matrix;
  29.         }
  30.  
  31.         static long BombMatrix(long targetRow, long targetCol,char power, long[,] matrix, long[,] newMatrix)
  32.         {
  33.             for (int row = 0; row < matrix.GetLength(0); row++)
  34.             {
  35.                 for (int col = 0; col < matrix.GetLength(1); col++)
  36.                 {
  37.                     bool isTarget = IsTarget(targetRow, targetCol, row, col);
  38.  
  39.                     if (isTarget)
  40.                     {
  41.                         if (newMatrix[row, col] < 0 && counter > 0)
  42.                         {
  43.                             break;
  44.                         }
  45.                         else
  46.                         {
  47.                             newMatrix[row, col] = matrix[row, col] - (long)Math.Ceiling((double)power / 2);
  48.                             counter = CellsDestroyed(newMatrix, counter, row, col);
  49.                            
  50.                         }
  51.                     }
  52.  
  53.                     if (row == targetRow && col == targetCol)
  54.                     {
  55.                         if (newMatrix[row, col] < 0 && counter > 0)
  56.                         {
  57.                             break;
  58.                         }
  59.                         else
  60.                         {
  61.                             newMatrix[row, col] = matrix[row, col] - power;
  62.  
  63.                             counter = CellsDestroyed(newMatrix, counter, row, col);
  64.                         }
  65.                     }
  66.                     else if (!isTarget)
  67.                     {
  68.                         newMatrix[row, col] = matrix[row, col];
  69.                     }
  70.                 }
  71.             }
  72.             return counter;
  73.         }
  74.  
  75.         static long CellsDestroyed(long[,] newMatrix, long counter, long row, long col)
  76.         {
  77.             if (newMatrix[row, col] <= 0)
  78.             {
  79.                 counter++;
  80.             }
  81.             return counter;
  82.         }
  83.         static bool IsTarget(long targetRow, long targetCol, long row, long col)
  84.         {
  85.             return (row == targetRow - 1 && col == targetCol - 1)
  86.             || (row == targetRow - 1 && col == targetCol)
  87.             || (row == targetRow - 1 && col == targetCol + 1)
  88.             || (row == targetRow && col == targetCol - 1)
  89.             || (row == targetRow && col == targetCol + 1)
  90.             || (row == targetRow + 1 && col == targetCol - 1)
  91.             || (row == targetRow + 1 && col == targetCol)
  92.             || (row == targetRow + 1 && col == targetCol + 1);
  93.         }
  94.  
  95.         static void Main(string[] args)
  96.         {
  97.             long[] dimension = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
  98.             long rows = dimension[0];
  99.             long cols = dimension[1];
  100.             long[,] matrix = new long[rows, cols];
  101.             long matrixSize = rows * cols;
  102.  
  103.  
  104.             ReadMatrix(rows, cols, matrix);
  105.  
  106.             string[] coordinates = Console.ReadLine().ToLower().Split(' ');
  107.  
  108.             long targetRow = long.Parse(coordinates[0]);
  109.             long targetCol = long.Parse(coordinates[1]);
  110.             char power = char.Parse(coordinates[2]);
  111.             long[,] newMatrix = new long[matrix.GetLength(0), matrix.GetLength(1)];
  112.  
  113.             while (!coordinates[0].Contains("cease"))
  114.             {
  115.                 BombMatrix(targetRow, targetCol,power,matrix,newMatrix);
  116.  
  117.                 targetRow = long.Parse(coordinates[0]);
  118.                 targetCol = long.Parse(coordinates[1]);
  119.                 power = char.Parse(coordinates[2]);
  120.                 coordinates = Console.ReadLine().ToLower().Split(' ');
  121.                 matrix = newMatrix;
  122.             }
  123.  
  124.             double result = Math.Round(counter / (double)(matrixSize) * 100,1);
  125.             Console.WriteLine("Destroyed bunkers: {0}", counter);
  126.             Console.WriteLine("Damage done: {0:F1} %",result);
  127.  
  128.             //PrintMatrix(matrix, newMatrix);
  129.         }
  130.  
  131.         static void PrintMatrix(long[,] matrix, long[,] newMatrix)
  132.         {
  133.             for (int row = 0; row < matrix.GetLength(0); row++)
  134.             {
  135.                 for (int col = 0; col < matrix.GetLength(1); col++)
  136.                 {
  137.                     Console.Write("{0,10}",newMatrix[row, col] + " ");
  138.                 }
  139.                 Console.WriteLine();
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement