Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. class P02TargetPractice
  4. {
  5.     static void Main()
  6.     {
  7.         string[] dimensions = Console.ReadLine().Split().ToArray();
  8.         int totalRows = int.Parse(dimensions[0]);
  9.         int totalCols = int.Parse(dimensions[1]);
  10.  
  11.         string snake = Console.ReadLine();
  12.         string[] shotParams = Console.ReadLine().Split().ToArray();
  13.         int impactRow = int.Parse(shotParams[0]);
  14.         int impactCol = int.Parse(shotParams[1]);
  15.         int raduis = int.Parse(shotParams[2]);
  16.  
  17.         char[,] matrix = new char[totalRows, totalCols];
  18.         int index = 0;
  19.         string direction = "left";
  20.         for (int row = totalRows - 1; row >= 0; row--)
  21.         {
  22.             if (direction == "left")
  23.             {
  24.                 int col = totalCols - 1;
  25.                 while (col >= 0)
  26.                 {
  27.                     if (index == snake.Length)
  28.                     {
  29.                         index = 0;
  30.                     }
  31.                     matrix[row, col] = snake[index];
  32.                     index++;
  33.                     col--;
  34.                 }
  35.                 direction = "right";
  36.             }
  37.             else if (direction == "right")
  38.             {
  39.                 int col = 0;
  40.                 while (col < totalCols)
  41.                 {
  42.                     if (index == snake.Length)
  43.                     {
  44.                         index = 0;
  45.                     }
  46.                     matrix[row, col] = snake[index];
  47.                     index++;
  48.                     col++;
  49.                 }
  50.                 direction = "left";
  51.             }
  52.         }
  53.  
  54.         for (int row = impactRow - raduis; row <= impactRow + raduis; row++)
  55.         {
  56.             if (row >=0 && row < totalRows)
  57.             {
  58.                 for (int col = impactCol - raduis; col <= impactCol + raduis; col++)
  59.                 {
  60.                     if (col >= 0 && col < totalCols)
  61.                     {
  62.                         if (IsCellWithinRadius(row, col, impactRow, impactCol, raduis))
  63.                         {
  64.                             matrix[row, col] = ' ';
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         for (int col = 0; col < totalCols; col++)
  72.         {
  73.             DropChar(matrix, col);
  74.         }
  75.  
  76.         for (int i = 0; i < totalRows; i++)
  77.         {
  78.             for (int j = 0; j < totalCols; j++)
  79.             {
  80.                 Console.Write(matrix[i, j]);
  81.             }
  82.             Console.WriteLine();
  83.         }
  84.     }
  85.  
  86.     static bool IsCellWithinRadius(int currRow, int currCol, int centerRow, int centerCol, int radius)
  87.     {
  88.         bool isInRange = false;
  89.         isInRange = ((currRow - centerRow) * (currRow - centerRow)) + ((currCol - centerCol) * (currCol - centerCol)) <= radius * radius;
  90.  
  91.         return isInRange;
  92.     }
  93.  
  94.     static void DropChar(char[,] matrix, int col)
  95.     {
  96.         while (true)
  97.         {
  98.             bool hasFallen = false;
  99.             for (int row = 1; row < matrix.GetLength(0); row++)
  100.             {
  101.                 char topChar = matrix[row - 1, col];
  102.                 char currentChar = matrix[row, col];
  103.                 if (currentChar == ' ' && topChar != ' ')
  104.                 {
  105.                     matrix[row, col] = topChar;
  106.                     matrix[row - 1, col] = ' ';
  107.                     hasFallen = true;
  108.                 }
  109.             }
  110.  
  111.             if (!hasFallen)
  112.             {
  113.                 break;
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement