Advertisement
jokerbg

Na Baba mi Smetalnika

Mar 8th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. class NaBabaMiSmetalnika
  4. {
  5.     static void ResetMatrix(int[,] matrix, int width)
  6.     {
  7.         int countOnes = 0;
  8.         for (int i = 0; i < 8; i++)
  9.         {
  10.             for (int j = 0; j < width; j++)
  11.             {
  12.                 if (matrix[i, j] == 1)
  13.                 {
  14.                     countOnes++;
  15.                     matrix[i, j] = 0;
  16.                 }
  17.             }
  18.             for (int j = 0; j < countOnes; j++)
  19.             {
  20.                 matrix[i, j] = 1;
  21.             }
  22.             countOnes = 0;
  23.         }
  24.     }
  25.     static void FingerLeft(int[,] matrix, int width, int row, int col)
  26.     {
  27.         if (col < 0)
  28.             col = 0;
  29.  
  30.         if (col > width - 1)
  31.             col = width - 1;
  32.  
  33.         int countOnes = 0;
  34.         for (int j = 0; j <= col; j++)
  35.         {
  36.             if (matrix[row, j] == 1)
  37.             {
  38.                 countOnes++;
  39.                 matrix[row, j] = 0;
  40.             }
  41.         }
  42.         for (int j = 0; j < countOnes; j++)
  43.         {
  44.             matrix[row, j] = 1;
  45.         }
  46.     }
  47.     static void FingerRight(int[,] matrix, int width, int row, int col)
  48.     {
  49.         if (col < 0)
  50.             col = 0;
  51.  
  52.         if (col > width - 1)
  53.             col = width - 1;
  54.  
  55.         int countOnes = 0;
  56.         for (int j = width - 1; j >= col; j--)
  57.         {
  58.             if (matrix[row, j] == 1)
  59.             {
  60.                 countOnes++;
  61.                 matrix[row, j] = 0;
  62.             }
  63.         }
  64.         for (int j = width - 1; j >= width - countOnes; j--)
  65.         {
  66.             matrix[row, j] = 1;
  67.         }
  68.     }
  69.     static BigInteger CheckEmptyCols(int[,] matrix, int width)
  70.     {
  71.         bool empty = true;
  72.         BigInteger result = 0;
  73.         for (int j = 0; j < width; j++)
  74.         {
  75.             for (int i = 0; i < 8; i++)
  76.             {
  77.                 if (matrix[i, j] == 1)
  78.                 {
  79.                     empty = false;
  80.                     break;
  81.                 }
  82.             }
  83.  
  84.             if (empty)
  85.             {
  86.                 result++;
  87.             }
  88.             else
  89.             {
  90.                 empty = true;
  91.             }
  92.         }
  93.         return result;
  94.     }
  95.     static BigInteger SumLines(int[,] matrix, int width)
  96.     {
  97.         BigInteger sumOfLines = 0;
  98.         string bild = "";
  99.         for (int i = 0; i < 8; i++)
  100.         {
  101.             for (int j = 0; j < width; j++)
  102.             {
  103.                 bild += matrix[i, j];
  104.             }
  105.             sumOfLines += (Convert.ToInt64(bild, 2));
  106.             bild = "";
  107.         }
  108.         return sumOfLines;
  109.     }
  110.     static void Main()
  111.     {
  112.         int width = Int32.Parse(Console.ReadLine());
  113.         int[,] abacus = new int[8, width];
  114.  
  115.         //fill abacus field
  116.         string[] arr = new string[8];
  117.         for (int i = 0; i < 8; i++)
  118.         {
  119.             arr[i] = Convert.ToString(int.Parse(Console.ReadLine()), 2).PadLeft(width, '0');
  120.             for (int j = 0; j < width; j++)
  121.             {
  122.                 abacus[i, j] = arr[i][j] == '0' ? 0 : 1;
  123.             }
  124.         }
  125.  
  126.         while (true)
  127.         {
  128.         pointer:
  129.             string command = Console.ReadLine();
  130.  
  131.             if (command == "stop")
  132.                 break;
  133.  
  134.             if (command == "reset")
  135.             {
  136.                 ResetMatrix(abacus, width);
  137.                 goto pointer;
  138.             }
  139.  
  140.             int fingerRow = Int32.Parse(Console.ReadLine());
  141.             int fingerCol = Int32.Parse(Console.ReadLine());
  142.  
  143.             if (command == "right")
  144.                 FingerRight(abacus, width, fingerRow, fingerCol);
  145.  
  146.             if (command == "left")
  147.                 FingerLeft(abacus, width, fingerRow, fingerCol);
  148.         }
  149.  
  150.         //print result
  151.         Console.WriteLine(CheckEmptyCols(abacus, width) * SumLines(abacus, width));
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement