Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class TargetPractice
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         var dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  10.         int row = dimensions[0];
  11.         int col = dimensions[1];
  12.         var matrix = new string[row][];
  13.         string snake = Console.ReadLine();
  14.         var shotParameters = Console.ReadLine().Split().Select(int.Parse).ToArray();
  15.  
  16.         FillMatrix(matrix, snake, col, row);
  17.  
  18.         ReplaceAllOutsideOfRadiusChars(matrix, shotParameters);
  19.  
  20.         //this is when the radius is == 0, so we just change the shot index to " "
  21.         if (shotParameters[2] == 0)
  22.         {
  23.             matrix[shotParameters[0]][shotParameters[1]] = " ";
  24.         }
  25.  
  26.         FallDownCharacters(matrix, row, col);
  27.  
  28.         for (int r = 0; r < row; r++)
  29.         {
  30.             for (int c = 0; c < col; c++)
  31.             {
  32.                 Console.Write(matrix[r][c]);
  33.             }
  34.             Console.WriteLine();
  35.         }
  36.  
  37.     }
  38.  
  39.     private static void FallDownCharacters(string[][] matrix, int row, int col)
  40.     {
  41.         var currentCol = new List<string>();
  42.  
  43.         for (int i = 0; i < col; i++)
  44.         {
  45.             for (int p = 0; p < row; p++)
  46.             {
  47.                 var currentSymbol = matrix[p][i];
  48.                 if (currentSymbol != " ")
  49.                 {
  50.                     currentCol.Add(currentSymbol);
  51.                 }
  52.             }
  53.  
  54.             int currentIndexOfCurrentCol = currentCol.Count - 1;
  55.             for (int c = row - 1; c >= 0; c--)
  56.             {
  57.                 if (currentIndexOfCurrentCol >= 0)
  58.                 {
  59.                     matrix[c][i] = currentCol[currentIndexOfCurrentCol];
  60.                     currentIndexOfCurrentCol--;
  61.                 }
  62.                 else
  63.                 {
  64.                     matrix[c][i] = " ";
  65.                 }
  66.             }
  67.             currentCol.Clear();
  68.         }
  69.     }
  70.  
  71.     private static void ReplaceAllOutsideOfRadiusChars(string[][] matrix, int[] shotParameters)
  72.     {
  73.         for (int i = 0; i < matrix.Length; i++)
  74.         {
  75.             for (int p = 0; p < matrix[i].Length; p++)
  76.             {
  77.                 if (PointInACircle(matrix, shotParameters, (double)i, (double)p))
  78.                 {
  79.                     matrix[i][p] = " ";
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     private static bool PointInACircle(string[][] matrix, int[] shotParameters, double x, double y)
  86.     {
  87.         var impactRow = shotParameters[0];
  88.         var impactCol = shotParameters[1];
  89.         var radius = shotParameters[2];
  90.         double d = Math.Pow((impactRow - x), radius) + Math.Pow((impactCol - y), radius);
  91.  
  92.         if (d > radius * radius)
  93.         {
  94.             return false;
  95.         }
  96.         else
  97.         {
  98.             return true;
  99.         }
  100.     }
  101.  
  102.     private static void FillMatrix(string[][] matrix, string snake, int col, int row)
  103.     {
  104.         int n = 0;
  105.         int currentIndex = 0;
  106.         for (int i = row - 1; i >= 0; i--, n++)
  107.         {
  108.             matrix[i] = new string[col];
  109.             if (n % 2 == 0)
  110.             {
  111.                 for (int p = col - 1; p >= 0; p--)
  112.                 {
  113.                     matrix[i][p] = snake[currentIndex].ToString();
  114.                     currentIndex++;
  115.                     if (currentIndex == snake.Length)
  116.                     {
  117.                         currentIndex = 0;
  118.                     }
  119.                 }
  120.             }
  121.             else
  122.             {
  123.                 for (int p = 0; p < col; p++)
  124.                 {
  125.                     matrix[i][p] = snake[currentIndex].ToString();
  126.                     currentIndex++;
  127.                     if (currentIndex == snake.Length)
  128.                     {
  129.                         currentIndex = 0;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement