Advertisement
fbinnzhivko

05.01 Bits at Crossroads

Mar 19th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 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.             int currentRow = row - 1;
  23.             int currentColumn = column + 1;
  24.  
  25.             // top-left
  26.             while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  27.             {
  28.                 if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  29.                 {
  30.                     numberOfCrossroads++;
  31.                 }
  32.  
  33.                 matrix[currentRow] |= 1 << currentColumn;
  34.  
  35.                 currentRow--;
  36.                 currentColumn++;
  37.             }
  38.  
  39.             currentRow = row + 1;
  40.             currentColumn = column - 1;
  41.  
  42.             // bottom-right
  43.             while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  44.             {
  45.                 if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  46.                 {
  47.                     numberOfCrossroads++;
  48.                 }
  49.  
  50.                 matrix[currentRow] |= 1 << currentColumn;
  51.  
  52.                 currentRow++;
  53.                 currentColumn--;
  54.             }
  55.  
  56.             currentRow = row + 1;
  57.             currentColumn = column + 1;
  58.  
  59.             // bottom-left
  60.             while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  61.             {
  62.                 if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  63.                 {
  64.                     numberOfCrossroads++;
  65.                 }
  66.  
  67.                 matrix[currentRow] |= 1 << currentColumn;
  68.  
  69.                 currentRow++;
  70.                 currentColumn++;
  71.             }
  72.  
  73.             currentRow = row - 1;
  74.             currentColumn = column - 1;
  75.  
  76.             // top-right
  77.             while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  78.             {
  79.                 if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  80.                 {
  81.                     numberOfCrossroads++;
  82.                 }
  83.  
  84.                 matrix[currentRow] |= 1 << currentColumn;
  85.  
  86.                 currentRow--;
  87.                 currentColumn--;
  88.             }
  89.  
  90.             command = Console.ReadLine();
  91.         }
  92.  
  93.         foreach (var number in matrix)
  94.         {
  95.             Console.WriteLine((uint)number);
  96.         }
  97.  
  98.         Console.WriteLine(numberOfCrossroads);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement