psotirov

Lines

Dec 10th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2.  
  3. class Lines
  4. {
  5.  
  6.     static void Main()
  7.     {
  8.         int[] numbers = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 }; // array of input numbers
  9.         int[] lines = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 }; // array of number of lines with length x -> lines[x-1]
  10.  
  11.  
  12.         for (int i = 0; i < 8; i++) // takes 8 lines with data
  13.         {
  14.             numbers[i] = int.Parse(Console.ReadLine()); // and puts them into the array
  15.         }
  16.  
  17.         for (int row = 0; row < 8; row++)
  18.         {
  19.             int HLineLength = 0;
  20.             int VLineLength = 0;
  21.             for (int col = 0; col < 8; col++)
  22.             {
  23.                 if (((numbers[row] >> col) & 1) == 1) // returns true if bit on col position of row element is set, i.e. cell(row, col)
  24.                 {
  25.                     HLineLength++; // increases the length of Horizontal Lines counter while the bit's sequence is set.
  26.                 }
  27.                 else // if horizontal line breaks
  28.                 {
  29.                     if (HLineLength > 0) lines[HLineLength-1]++; // if we have line with positive length counts it
  30.                     HLineLength = 0;
  31.                 }
  32.  
  33.                 if (((numbers[col] >> row) & 1) == 1) // returns true if bit on row position of col element is set, i.e. cell(col, row)
  34.                 {
  35.                     VLineLength++; // increases the length of Vertical Lines counter while the bit's sequence is set.
  36.                 }
  37.                 else // if vertical line breaks
  38.                 {
  39.                     if (VLineLength > 1) lines[VLineLength-1]++; // if we have line with positive length counts it
  40.                     VLineLength = 0; // also avoids duplicate counting of lines with length of 1
  41.                 }
  42.             }
  43.             if (HLineLength > 0) lines[HLineLength-1]++; // if we have a final horizontal line with positive length counts it
  44.             if (VLineLength > 1) lines[VLineLength-1]++; // if we have a final horizontal line with positive length counts it
  45.         }
  46.  
  47.         for (int i = 7; i >=0; i--)
  48.             if (lines[i]>0)
  49.                 {
  50.                     Console.WriteLine(i+1);
  51.                     Console.WriteLine(lines[i]);
  52.                     break;
  53.                 }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment