fbinnzhivko

Bits at Crossroads

Mar 19th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System;
  2.  
  3. public class BitsAtCrossroads
  4. {
  5.     public static void Main()
  6.     {
  7.         int size = int.Parse(Console.ReadLine());
  8.         int[] matrix = new int[size];
  9.         int numberOfCrossroads = 0;
  10.  
  11.         string command = Console.ReadLine();
  12.  
  13.         while (command != "end")
  14.         {
  15.             int row = int.Parse(command.Split()[0]);
  16.             int column = int.Parse(command.Split()[1]);
  17.  
  18.             numberOfCrossroads++;
  19.  
  20.             matrix[row] |= 1 << column;
  21.  
  22.             // top-left
  23.             FillPath(row - 1, column + 1, matrix, -1, 1, ref numberOfCrossroads);
  24.  
  25.             // bottom-right
  26.             FillPath(row + 1, column - 1, matrix, 1, -1, ref numberOfCrossroads);
  27.  
  28.             // bottom-left
  29.             FillPath(row + 1, column + 1, matrix, 1, 1, ref numberOfCrossroads);
  30.  
  31.             // top-right
  32.             FillPath(row - 1, column - 1, matrix, -1, -1, ref numberOfCrossroads);
  33.  
  34.             command = Console.ReadLine();
  35.         }
  36.  
  37.         foreach (var number in matrix)
  38.         {
  39.             Console.WriteLine((uint)number);
  40.         }
  41.  
  42.         Console.WriteLine(numberOfCrossroads);
  43.     }
  44.  
  45.     private static void FillPath(
  46.         int currentRow,
  47.         int currentColumn,
  48.         int[] matrix,
  49.         int rowUpdate,
  50.         int colUpdate,
  51.         ref int numberOfCrossroads)
  52.     {
  53.         while (AreCoordinatesValid(currentRow, currentColumn, matrix.Length))
  54.         {
  55.             if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  56.             {
  57.                 numberOfCrossroads++;
  58.             }
  59.  
  60.             matrix[currentRow] |= 1 << currentColumn;
  61.  
  62.             currentRow += rowUpdate;
  63.             currentColumn += colUpdate;
  64.         }
  65.     }
  66.  
  67.     private static bool AreCoordinatesValid(int row, int col, int size)
  68.     {
  69.         bool isRowValid = 0 <= row && row < size;
  70.         bool isColumnValid = 0 <= col && col < size;
  71.  
  72.         return isRowValid && isColumnValid;
  73.     }
  74. }
Add Comment
Please, Sign In to add comment