Advertisement
fbinnzhivko

04.Text Bonbardmment

Mar 12th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Text_Bombardment
  5. {
  6.     class TextBombardment
  7.     {
  8.         static void Main()
  9.         {
  10.             // Get the matrix params
  11.             string text = Console.ReadLine();
  12.             int cols = int.Parse(Console.ReadLine());
  13.             int rows = text.Length / cols;
  14.             rows += text.Length % cols == 0 ? 0 : 1;
  15.            
  16.             // Get the bomb params
  17.             string bombs = Console.ReadLine();
  18.             int[] bombCols = bombs.Split(' ').Select(int.Parse).ToArray();
  19.  
  20.             // Build the matrix with the text
  21.             char[,] matrix = new char[rows, cols];
  22.             for (int row = 0, letter = 0; row < rows; row++)
  23.             {
  24.                 for (int col = 0; letter < text.Length && col < cols; col++, letter++)
  25.                 {
  26.                     matrix[row, col] = text[letter];
  27.                 }
  28.             }
  29.  
  30.             // Bombard the text
  31.             for (int col = 0; col < bombCols.Length; col++)
  32.             {
  33.                 int bombCol = bombCols[col];
  34.                 Bombard(matrix, bombCol);
  35.             }
  36.  
  37.             // Print the remaining text
  38.             for (int row = 0; row < rows; row++)
  39.             {
  40.                 for (int col = 0; col < cols; col++)
  41.                 {
  42.                     char letter = matrix[row, col];
  43.                     if (letter != '\0')
  44.                     {
  45.                         Console.Write(matrix[row, col]);
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private static void Bombard(char[,] matrix, int col)
  52.         {
  53.             int startRow = 0;
  54.             int endRow = 0;
  55.  
  56.             // Get the rows whose cols should be cleared
  57.             for (int row = 0; row < matrix.GetLength(0); row++)
  58.             {
  59.                 if (matrix[row, col] != ' ')
  60.                 {
  61.                     startRow = row;
  62.                     while (row < matrix.GetLength(0) && matrix[row, col] != ' ')
  63.                     {
  64.                         row++;
  65.                     }
  66.                     endRow = row - 1;
  67.                     break;
  68.                 }
  69.             }
  70.  
  71.             // Clear the bombed positions
  72.             int currentRow = startRow;
  73.             while (currentRow <= endRow)
  74.             {
  75.                 matrix[currentRow, col] = ' ';
  76.                 currentRow++;
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement