Advertisement
amphibia89

Bunker Buster 60/100

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