Advertisement
Guest User

06.TargetPractice

a guest
Jun 3rd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 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.  
  8. public class Program
  9. {
  10.     public static void Main()
  11.     {
  12.         int[] matrixArgs = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13.             .Select(int.Parse).ToArray();
  14.         int rows = matrixArgs[0];
  15.         int cols = matrixArgs[1];
  16.         string snakeString = Console.ReadLine().Trim();
  17.         int[] shotParameters = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  18.             .Select(int.Parse).ToArray();
  19.         int impactRow = shotParameters[0];
  20.         int impactCol = shotParameters[1];
  21.         int radius = shotParameters[2];
  22.         char[,] matrix = new char[rows, cols];
  23.         int snakeIndexCounter = 0;
  24.  
  25.         //Filling in the matrix based on the required pattern
  26.         for (int rowIndex = rows - 1; rowIndex >= 0; rowIndex--)
  27.         {
  28.             if (rowIndex % 2 == 0)
  29.             {
  30.                 for (int colIndex = cols - 1; colIndex >= 0; colIndex--)
  31.                 {
  32.                     matrix[rowIndex, colIndex] = snakeString[snakeIndexCounter];
  33.                     snakeIndexCounter++;
  34.                     if (snakeIndexCounter == snakeString.Length)
  35.                     {
  36.                         snakeIndexCounter = 0;
  37.                     }
  38.                    
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  44.                 {
  45.                     matrix[rowIndex, colIndex] = snakeString[snakeIndexCounter];
  46.                     snakeIndexCounter++;
  47.                     if (snakeIndexCounter == snakeString.Length)
  48.                     {
  49.                         snakeIndexCounter = 0;
  50.                     }
  51.                 }
  52.             }
  53.        
  54.         }
  55.  
  56.         //Replace all characters in the blast area with a space
  57.         for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  58.         {
  59.             for (int colIndex = 0; colIndex < cols; colIndex++)
  60.             {
  61.                 if ((rowIndex - impactRow)*(rowIndex - impactRow) + (colIndex - impactCol)*(colIndex - impactCol) <= radius*radius)
  62.                 {
  63.                     matrix[rowIndex, colIndex] = ' ';
  64.                 }
  65.             }
  66.         }
  67.  
  68.         //All characters start falling down until they land on other characters
  69.         for (int coIndex = 0; coIndex < cols; coIndex++)
  70.         {
  71.             char[] temp = new char[rows];
  72.  
  73.             for (int rowsIndex = 0; rowsIndex < rows; rowsIndex++)
  74.             {
  75.                 temp[rowsIndex] = matrix[rowsIndex, coIndex];
  76.             }
  77.  
  78.             temp = temp.OrderByDescending(x => x == ' ').ToArray();
  79.  
  80.             for (int rowsIndex = 0; rowsIndex < rows; rowsIndex++)
  81.             {
  82.                 matrix[rowsIndex, coIndex] = temp[rowsIndex];
  83.             }
  84.         }
  85.  
  86.         //Print out the final matrix
  87.         for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  88.         {
  89.             for (int colIndex = 0; colIndex < cols; colIndex++)
  90.             {
  91.                 Console.Write(matrix[rowIndex, colIndex]);
  92.             }
  93.             Console.WriteLine();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement