Advertisement
sylviapsh

Pillars

Dec 27th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. class Pillars
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //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. 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 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 (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 square 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 = 0
  10. //1     ■                         n1 = 64
  11. //2                                 n2 = 0
  12. //3                 ■             n3 = 8
  13. //4                                 n4 = 0
  14. //5                 ■ ■         n5 = 12
  15. //6 ■ ■ ■                     n6 = 224
  16. //7                                 n7 = 0
  17. //We are allowed to put a vertical pillar over any of the columns in the grid. Pillars split the grid into two sides (left and right) and the column holding the pillar is ignored. Write a program that finds the leftmost column where the pillar can be put so that the full cells on the left side and on the right side are equal number. For example at the figure if we put the pillar at column 5, it will split the grid into two sides and both sides will have exactly 3 full cells.
  18.  
  19.     int[,] grid = new int[8, 8];
  20.    
  21.     for (int row = 0; row < 8; row++)
  22.     {
  23.       int number = int.Parse(Console.ReadLine());
  24.       for (int column = 7; column >= 0; column--)
  25.       {
  26.         grid[row, column] = number % 2;
  27.         number /= 2;
  28.       }
  29.     }
  30.    
  31.     int sumLeft = 0;
  32.     int sumRight = 0;
  33.     int resultSum = 0;
  34.     int resultPillar = 9; //9 so that there is no such pillar possible. If there is then the resultPillar will get its value.
  35.  
  36.  
  37.     for (int pillar = 7; pillar >= 0; pillar--)
  38.     {
  39.       sumRight = 0;
  40.       sumLeft = 0;
  41.  
  42.       for (int row = 0; row <= 7; row++)  //Left sums
  43.       {
  44.         for (int column = pillar + 1; column <= 7; column++)
  45.         {
  46.           if (grid[row, column] == 1)
  47.           {
  48.             sumLeft ++;
  49.           }
  50.         }
  51.       }
  52.  
  53.       for (int row = 0; row <= 7; row++) //Right sums
  54.       {
  55.         for (int column = 0; column <= pillar - 1; column++)
  56.         {
  57.           if (grid[row, column] == 1)
  58.           {
  59.             sumRight ++;
  60.           }
  61.         }
  62.       }
  63.       if (sumLeft == sumRight & pillar < resultPillar)
  64.       {
  65.         resultPillar = pillar;
  66.         resultSum = sumRight;
  67.       }
  68.     }
  69.     if (resultPillar < 9)
  70.     {
  71.       Console.WriteLine(7-resultPillar);
  72.       Console.WriteLine(resultSum);
  73.     }
  74.     else
  75.     {
  76.       Console.WriteLine("No");
  77.     }
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement