Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace TextGravity
- {
- using System;
- using System.Text;
- using System.Security;
- public class TextGravity
- {
- public static void Main()
- {
- int numberOfColumns = int.Parse(Console.ReadLine());
- string inputLine = Console.ReadLine();
- int numberOfRows = inputLine.Length / numberOfColumns;
- if (inputLine.Length % numberOfColumns != 0)
- {
- numberOfRows++;
- }
- char[,] matrix = new char[numberOfRows, numberOfColumns];
- //Fill the matrix
- int currentCharIndex = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (currentCharIndex < inputLine.Length)
- {
- matrix[row, col] = inputLine[currentCharIndex];
- currentCharIndex++;
- }
- else
- {
- matrix[row, col] = ' ';
- }
- }
- }
- //Text fall
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- RunGravity(matrix, col);
- }
- //Print
- StringBuilder outputBuilder = new StringBuilder();
- outputBuilder.Append("<table>");
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- outputBuilder.Append("<tr>");
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- outputBuilder.AppendFormat("<td>{0}</td>",
- SecurityElement.Escape(matrix[row, col].ToString()));
- }
- outputBuilder.Append("</tr>");
- }
- outputBuilder.Append("</table>");
- Console.WriteLine(outputBuilder.ToString());
- }
- //Method for falling
- private static void RunGravity(char[,] matrix, int col)
- {
- while (true)
- {
- bool hasFalen = false;
- for (int row = 1; row < matrix.GetLength(0); row++)
- {
- char topChar = matrix[row - 1, col];
- char currentChar = matrix[row, col];
- if (currentChar == ' ' && topChar != ' ')
- {
- matrix[row, col] = topChar;
- matrix[row - 1, col] = ' ';
- hasFalen = true;
- }
- }
- if (! hasFalen)
- {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment