Advertisement
sashomaga

Lines

Dec 24th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2.  
  3. //You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns.
  4. //Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last line
  5. //is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered from the first
  6. //(top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7. The figure shows a sample square
  7. //grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
  8. //    7 6   5   4   3   2   1   0  
  9. //0                 ■             n0 = 8
  10. //1     ■         ■                      n1 = 72
  11. //2                 ■             n2 = 8
  12. //3                 ■             n3 = 8
  13. //4             ■                 n4 = 16
  14. //5             ■ ■ ■         n5 = 28
  15. //6 ■ ■ ■ ■                 n6 = 240
  16. //7                                 n7 = 0
  17. //A line is any sequence of full cells staying on the same row or column. At the figure above we have two lines of 4 cells and two lines of 3 cells and one line of 1 cell.
  18. //You need to create a program that finds the longest line in the grid and the number of lines with the longest length. At the figure we have two largest lines with
  19. //length of 4 cells.
  20. //Input
  21. //The input data is should be read from the console. There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7. It is guaranteed that there
  22. //exists at least one line in the grid (the grid is not empty).
  23. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  24. //Output
  25. //The output consists of two integers placed on separate lines. The first line should hold the length of the longest line in the grid. The second line should
  26. //hold the number of lines with the maximal length.
  27.  
  28.  
  29. class Program
  30. {
  31.     static void Main()
  32.     {
  33.         int[] num = new int[8];
  34.         int[,] matrix = new int[8, 8];
  35.  
  36.         for (int i = 0; i < num.Length; i++)
  37.         {
  38.             num[i] = int.Parse(Console.ReadLine());
  39.             for (int j = 0, reverse = num.Length - 1; j < num.Length; j++, reverse--)
  40.             {
  41.                 if ((num[i] >> j & 1) == 1)
  42.                 {
  43.                     matrix[i, reverse] = 1;
  44.                 }
  45.             }
  46.         }
  47.         //check y
  48.         int line = new int();
  49.         int count = new int();
  50.         int maxLine = new int();
  51.         for (int x = 0; x < num.Length; x++)
  52.         {
  53.             for (int y = 0; y < num.Length; y++)
  54.             {
  55.                 if (matrix[x, y] == 1)
  56.                 {
  57.                     line += 1;
  58.                     if (line == maxLine)
  59.                     {
  60.                         count++;
  61.                     }
  62.                     if (line > maxLine)
  63.                     {
  64.                         maxLine = line;
  65.                         count = 1;
  66.                     }
  67.                 }
  68.                 else
  69.                 {
  70.                     line = 0;
  71.                 }
  72.             }
  73.             line = 0;
  74.         }
  75.         //check x
  76.         for (int y = 0; y < num.Length; y++)
  77.         {
  78.             for (int x = 0; x < num.Length; x++)
  79.             {
  80.                 if (matrix[x, y] == 1)
  81.                 {
  82.                     line += 1;
  83.                     if (line == maxLine)
  84.                     {
  85.                         count++;
  86.                     }
  87.                     if (line > maxLine)
  88.                     {
  89.                         maxLine = line;
  90.                         count = 1;
  91.                     }
  92.                 }
  93.                 else
  94.                 {
  95.                     line = 0;
  96.                 }
  97.             }
  98.             line = 0;
  99.         }
  100.         ////print
  101.         //for (int i = 0; i < num.Length; i++)
  102.         //{
  103.         //    for (int j = 0; j < num.Length; j++)
  104.         //    {
  105.         //        Console.Write(matrix[i, j]);
  106.         //    }
  107.         //    Console.WriteLine();
  108.         //}
  109.  
  110.         Console.WriteLine(maxLine);
  111.         if (maxLine == 1)
  112.         {
  113.             count /= 2;
  114.         }
  115.         Console.WriteLine(count);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement