Advertisement
social1986

Untitled

Jan 28th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _6._Target_Practice
  5. {
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             var sizes = Console.ReadLine()
  11.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             var rows = sizes[0];
  16.             var cols = sizes[1];
  17.             var snakeString = Console.ReadLine();
  18.             var matrix = new char[rows,cols];
  19.  
  20.             FillingStairsWithSnakes(matrix, snakeString);            
  21.  
  22.             var bombPositionAndRadius = Console.ReadLine()
  23.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  24.                 .Select(int.Parse)
  25.                 .ToArray();
  26.  
  27.             KillingTheSnakes(matrix, bombPositionAndRadius);
  28.  
  29.             RearangeMatrix(matrix);
  30.  
  31.             PrintTheMatrix(matrix);
  32.            
  33.         }
  34.  
  35.         public static void FillingStairsWithSnakes(char[,]matrix, string snakeString)
  36.         {
  37.             var snakeIndex = 0;
  38.             var rowIndex = 0;
  39.  
  40.             for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
  41.             {
  42.                 if (rowIndex % 2 == 0)
  43.                 {
  44.                     for (int col = matrix.GetLength(1) - 1; col >= 0; col--)
  45.                     {
  46.                         matrix[row, col] = snakeString[snakeIndex++ % snakeString.Length];
  47.                     }
  48.                     rowIndex++;
  49.                 }
  50.                 else
  51.                 {
  52.                     for (int col = 0; col < matrix.GetLength(1); col++)
  53.                     {
  54.                         matrix[row, col] = snakeString[snakeIndex++ % snakeString.Length];
  55.                     }
  56.                     rowIndex++;
  57.                 }
  58.  
  59.             }
  60.         }
  61.  
  62.         public static void KillingTheSnakes(char[,] matrix, int[] bombPositionAndRadius)
  63.         {
  64.             var bombRadius = bombPositionAndRadius[2];
  65.             var bombRow = bombPositionAndRadius[0];
  66.             var bombCol = bombPositionAndRadius[1];
  67.  
  68.             for (int row = 0; row < matrix.GetLength(0); row++)
  69.             {
  70.                 for (int col = 0; col < matrix.GetLength(1); col++)
  71.                 {
  72.                     var a = bombRow - row;
  73.                     var b = bombCol - col;
  74.  
  75.                     double distance = Math.Sqrt(a * a + b * b);
  76.                     if (distance <= bombRadius)
  77.                     {
  78.                         matrix[row, col] = ' ';
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         public static void RearangeMatrix(char[,] matrix)
  85.         {
  86.             for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
  87.             {
  88.                 for (int col = matrix.GetLength(1) - 1; col >= 0; col--)
  89.                 {
  90.                     if (matrix[row, col] == ' ')
  91.                     {
  92.                         var currentRow = row;
  93.  
  94.                         while (matrix[currentRow, col] == ' ' && currentRow > 0)
  95.                         {
  96.                             matrix[row, col] = matrix[--currentRow, col];
  97.                         }
  98.                         matrix[currentRow, col] = ' ';
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.  
  104.         public static void PrintTheMatrix(char[,] matrix)
  105.         {
  106.             for (int row = 0; row < matrix.GetLength(0); row++)
  107.             {
  108.                 for (int col = 0; col < matrix.GetLength(1); col++)
  109.                 {
  110.                     Console.Write(matrix[row,col]);
  111.                 }
  112.                 Console.WriteLine();
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement