Advertisement
dimipan80

Fire the Arrows

May 16th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. /* We are given a matrix containing arrows that need to be moved. The arrows are the following: '<' (ASCII code 60), '>' (ASCII code 62), '^' (ASCII code 94), 'v' (ASCII code 118). Respectively pointing left, right, up and down. There are also blank spaces that are indicated by 'o' (ASCII code 111). There will be no other characters in the matrix.
  2.  * Your task is to move all arrows, one at a time, in the direction they point to until there are no more possible moves.
  3.  * The arrows should be moved in the following order: first the ones in the uppermost row and the leftmost column.
  4.  * At the first input line you will be given a number n specifying how many rows after it will follow. At the next n lines you will be given the matrix with the arrows that need to be moved.
  5.  * The output should be the new matrix with all the arrows moved to the direction they're facing. */
  6.  
  7. namespace FireTheArrows
  8. {
  9.     using System;
  10.  
  11.     class FireTheArrows
  12.     {
  13.         private static char[][] matrix;
  14.  
  15.         private static bool hasMoved;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             int rows = int.Parse(Console.ReadLine());
  20.             matrix = new char[rows][];
  21.             for (int i = 0; i < matrix.Length; i++)
  22.             {
  23.                 matrix[i] = Console.ReadLine().ToCharArray();
  24.             }
  25.  
  26.             hasMoved = true;
  27.             while (hasMoved)
  28.             {
  29.                 MoveAllArrowsOneTime();
  30.             }
  31.  
  32.             foreach (char[] line in matrix)
  33.             {
  34.                 Console.WriteLine(string.Join(string.Empty, line));
  35.             }
  36.         }
  37.  
  38.         private static void MoveAllArrowsOneTime()
  39.         {
  40.             hasMoved = false;
  41.             for (int row = 0; row < matrix.Length; row++)
  42.             {
  43.                 for (int col = 0; col < matrix[row].Length; col++)
  44.                 {
  45.                     if (matrix[row][col] != (char)111)
  46.                     {
  47.                         int dirRow = 0, dirCol = 0;
  48.                         int arrow = matrix[row][col];
  49.                         switch (arrow)
  50.                         {
  51.                             case 60:
  52.                                 dirCol = -1;
  53.                                 break;
  54.                             case 62:
  55.                                 dirCol = 1;
  56.                                 break;
  57.                             case 94:
  58.                                 dirRow = -1;
  59.                                 break;
  60.                             case 118:
  61.                                 dirRow = 1;
  62.                                 break;
  63.                         }
  64.  
  65.                         int nextRow = row + dirRow;
  66.                         int nextCol = col + dirCol;
  67.                         if (nextRow >= 0 && nextRow < matrix.Length &&
  68.                             nextCol >= 0 && nextCol < matrix[row].Length &&
  69.                             matrix[nextRow][nextCol] == (char)111)
  70.                         {
  71.                             matrix[nextRow][nextCol] = (char)arrow;
  72.                             matrix[row][col] = (char)111;
  73.                             hasMoved = true;
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement