stefan1919

Bits as Crossroads

Aug 27th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int size = int.Parse(Console.ReadLine());
  14.             int[] matrix = new int[size];
  15.             int numberOfCrossroads = 0;
  16.  
  17.             string command = Console.ReadLine();
  18.  
  19.             while (command != "end")
  20.             {
  21.                 int row = int.Parse(command.Split()[0]);
  22.                 int column = int.Parse(command.Split()[1]);
  23.  
  24.                 numberOfCrossroads++;
  25.  
  26.                 matrix[row] |= 1 << column;
  27.  
  28.                 int currentRow = row - 1;
  29.                 int currentColumn = column + 1;
  30.  
  31.                 // top-left
  32.                 while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  33.                 {
  34.                     if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  35.                     {
  36.                         numberOfCrossroads++;
  37.                     }
  38.  
  39.                     matrix[currentRow] |= 1 << currentColumn;
  40.  
  41.                     currentRow--;
  42.                     currentColumn++;
  43.                 }
  44.  
  45.                 currentRow = row + 1;
  46.                 currentColumn = column - 1;
  47.  
  48.                 // bottom-right
  49.                 while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  50.                 {
  51.                     if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  52.                     {
  53.                         numberOfCrossroads++;
  54.                     }
  55.  
  56.                     matrix[currentRow] |= 1 << currentColumn;
  57.  
  58.                     currentRow++;
  59.                     currentColumn--;
  60.                 }
  61.  
  62.                 currentRow = row + 1;
  63.                 currentColumn = column + 1;
  64.  
  65.                 // bottom-left
  66.                 while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  67.                 {
  68.                     if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  69.                     {
  70.                         numberOfCrossroads++;
  71.                     }
  72.  
  73.                     matrix[currentRow] |= 1 << currentColumn;
  74.  
  75.                     currentRow++;
  76.                     currentColumn++;
  77.                 }
  78.  
  79.                 currentRow = row - 1;
  80.                 currentColumn = column - 1;
  81.  
  82.                 // top-right
  83.                 while (0 <= currentRow && currentRow < size && 0 <= currentColumn && currentColumn < size)
  84.                 {
  85.                     if (((matrix[currentRow] >> currentColumn) & 1) == 1)
  86.                     {
  87.                         numberOfCrossroads++;
  88.                     }
  89.  
  90.                     matrix[currentRow] |= 1 << currentColumn;
  91.  
  92.                     currentRow--;
  93.                     currentColumn--;
  94.                 }
  95.  
  96.                 command = Console.ReadLine();
  97.             }
  98.  
  99.             foreach (var number in matrix)
  100.             {
  101.                 Console.WriteLine((uint)number);
  102.             }
  103.  
  104.             Console.WriteLine(numberOfCrossroads);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment