Simooo

Bits at Crossroads

Jul 4th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2.  
  3. class Crossroads
  4. {
  5.     static void Main()
  6.     {
  7.         int sizeOfBoard = int.Parse(Console.ReadLine());
  8.         int[,] matrix = new int[sizeOfBoard, sizeOfBoard];
  9.  
  10.         string input = Console.ReadLine();
  11.         int crossroads = 0;
  12.         while (input != "end")
  13.         {
  14.             string[] predefinedCrossroad = input.Split(' ');
  15.             int line = int.Parse(predefinedCrossroad[0]);
  16.             int position = int.Parse(predefinedCrossroad[1]);
  17.             if (line == 0 || line == sizeOfBoard - 1 || position == 0 || position == sizeOfBoard - 1)
  18.             {
  19.                 crossroads++; // predefined crossroad which will not be detected in the scan later.
  20.             }
  21.             int col = position;
  22.             int row = line;
  23.  
  24.             while (col >= 0 && row <= sizeOfBoard - 1) // filling the matrix to bottom right
  25.             {
  26.  
  27.                 matrix[row, col] = 1;
  28.                 row++;
  29.                 col--;
  30.             }
  31.             col = position;
  32.             row = line;
  33.             while (col <= sizeOfBoard - 1 && row >= 0) // filling the matrix to upper left
  34.             {
  35.                 matrix[row, col] = 1;
  36.                 row--;
  37.                 col++;
  38.             }
  39.             col = position;
  40.             row = line;
  41.             while (col >= 0 && row >= 0) // filling the matrix to upper right
  42.             {
  43.                 matrix[row, col] = 1;
  44.                 row--;
  45.                 col--;
  46.  
  47.             }
  48.             col = position;
  49.             row = line;
  50.             while (col <= sizeOfBoard - 1 && row <= sizeOfBoard - 1) // filling to bottom left
  51.             {
  52.                 matrix[row, col] = 1;
  53.                 row++;
  54.                 col++;
  55.             }
  56.             input = Console.ReadLine();
  57.  
  58.         }
  59.         string binary = "";
  60.         long integer = 0;
  61.         for (int row = 0; row <= sizeOfBoard - 1; row++)
  62.         {
  63.             for (int col = sizeOfBoard - 1; col >= 0; col--)
  64.             {
  65.                
  66.                 binary += matrix[row, col]; // getting the string representation of the binary number
  67.                 if ((row > 0 && row < sizeOfBoard - 1) && (col > 0 && col < sizeOfBoard - 1))
  68.                 {
  69.                     if (matrix[row, col] == 1 && matrix[row + 1, col + 1] == 1 && matrix[row - 1, col + 1] == 1 && matrix[row + 1, col - 1] == 1 && matrix[row - 1, col - 1] == 1)
  70.                     {
  71.                         crossroads++; // checking for a crossroad
  72.                     }
  73.                 }
  74.                
  75.  
  76.                
  77.             }
  78.             Console.WriteLine(integer = Convert.ToInt64(binary, 2)); // binary to integer
  79.            
  80.             binary = ""; // getting the binary again to default value because otherwise all numbers will stack.
  81.  
  82.         }
  83.         Console.WriteLine(crossroads);
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment