Advertisement
dimipan80

Clearing Commands

May 25th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. /* You are given a matrix, holding 1 or more strings of ASCII characters. The strings will come from the console, each on a separate line. These strings will contain all the garbage and commands by the SoftUni team. Your task is to find and execute all the clearing commands within the matrix. The commands are initiated by the following characters:
  2.  * 1.   ">" – Removes the characters to the right and replaces each one with a single space (" ").
  3.  * 2.   "<" – Removes the characters to the left and replaces each one with a single space (" ").
  4.  * 3.   "v" – Removes the characters below and replaces each one with a single space (" ").
  5.  * 4.   "^" – Removes the characters above and replaces each one with a single space (" ").
  6.  * Every command is executed until it reaches another command character or the matrix borders are reached.
  7.  * The output should consist of <p> tags, each holding a row of the matrix (a string). Make sure you escape all the special characters within the <p> tags with the SecurityElement.Escape method. */
  8.  
  9. namespace Clearing_Commands
  10. {
  11.     using System;
  12.     using System.Collections.Generic;
  13.     using System.Linq;
  14.     using System.Security;
  15.  
  16.     class ClearingCommands
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             List<char[]> matrix = new List<char[]>();
  21.             string inputLine = Console.ReadLine();
  22.             while (inputLine != "END")
  23.             {
  24.                 matrix.Add(inputLine.ToCharArray());
  25.                 inputLine = Console.ReadLine();
  26.             }
  27.  
  28.             for (int row = 0; row < matrix.Count; row++)
  29.             {
  30.                 for (int col = 0; col < matrix[row].Length; col++)
  31.                 {
  32.                     switch (matrix[row][col])
  33.                     {
  34.                         case '<':
  35.                             ExecuteClearingCommand(matrix, row, col, 0, -1);
  36.                             break;
  37.                         case '>':
  38.                             ExecuteClearingCommand(matrix, row, col, 0, 1);
  39.                             break;
  40.                         case 'v':
  41.                             ExecuteClearingCommand(matrix, row, col, 1, 0);
  42.                             break;
  43.                         case '^':
  44.                             ExecuteClearingCommand(matrix, row, col, -1, 0);
  45.                             break;
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             PrintMatrix(matrix);
  51.         }
  52.  
  53.         private static void ExecuteClearingCommand(List<char[]> matrix, int row, int col, int dirY, int dirX)
  54.         {
  55.             row += dirY;
  56.             col += dirX;
  57.             string commands = "<>v^";
  58.             while (IsInsideInTheMatrix(matrix, row, col) &&
  59.                 !commands.Contains(matrix[row][col]))
  60.             {
  61.                 matrix[row][col] = ' ';
  62.                 row += dirY;
  63.                 col += dirX;
  64.             }
  65.         }
  66.  
  67.         private static bool IsInsideInTheMatrix(List<char[]> matrix, int row, int col)
  68.         {
  69.             return row >= 0 && row < matrix.Count && col >= 0 && col < matrix[row].Length;
  70.         }
  71.  
  72.         private static void PrintMatrix(List<char[]> matrix)
  73.         {
  74.             foreach (char[] line in matrix)
  75.             {
  76.                 Console.WriteLine("<p>{0}</p>",
  77.                     SecurityElement.Escape(new string(line)));
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement