Advertisement
Svetli0o

NaBabaMiSmetaloto

Apr 1st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9.     private static int[,] Reset(int[,] matrix, int length)
  10.     {
  11.         for (int i = 1; i < length; i++)
  12.         {
  13.             for (int j = 0; j < 8; j++)
  14.             {
  15.                 if (matrix[j, i] == 1)
  16.                 {
  17.                     int cellCol = i;
  18.                     while (true)
  19.                     {
  20.                         if (cellCol == 0 || matrix[j, cellCol - 1] == 1)
  21.                         {
  22.                             matrix[j, i] = 0;
  23.                             matrix[j, cellCol] = 1;
  24.                             break;
  25.                         }
  26.                         cellCol--;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         return matrix;
  32.     }
  33.     private static int[,] MoveLeft(int[,] matrix, int startRow, int startCol)
  34.     {
  35.         for (int i = 1; i <= startCol; i++)
  36.         {
  37.             if (matrix[startRow, i] == 1)
  38.             {
  39.                 int cellCol = i;
  40.                 while (true)
  41.                 {
  42.                     if (cellCol == 0 || matrix[startRow, cellCol - 1] == 1)
  43.                     {
  44.                         matrix[startRow, i] = 0;
  45.                         matrix[startRow, cellCol] = 1;
  46.                         break;
  47.                     }
  48.                     cellCol--;
  49.                 }
  50.             }
  51.         }
  52.         return matrix;
  53.     }
  54.     private static int[,] MoveRight(int[,] matrix, int startRow, int startCol, int length)
  55.     {
  56.         for (int i = length - 1; i >= startCol; i--)
  57.         {
  58.             if (matrix[startRow, i] == 1)
  59.             {
  60.                 int cellCol = i;
  61.                 while (true)
  62.                 {
  63.                     if (cellCol == length - 1 || matrix[startRow, cellCol + 1] == 1)
  64.                     {
  65.                         matrix[startRow, i] = 0;
  66.                         matrix[startRow, cellCol] = 1;
  67.                         break;
  68.                     }
  69.                     cellCol++;
  70.                 }
  71.             }
  72.         }
  73.         return matrix;
  74.     }
  75.     private static ulong Count(int[,] matrix, int length)
  76.     {
  77.         ulong countOfZeroColumns = 0;
  78.         ulong sum = 0;
  79.         //count the sum of the columns
  80.         for (int i = 0; i < 8; i++)
  81.         {
  82.             string binaryString = "";
  83.             for (int j = 0; j < length; j++)
  84.             {
  85.                 binaryString += matrix[i, j].ToString();
  86.             }
  87.             sum += Convert.ToUInt64(binaryString, 2);
  88.         }
  89.         //count the zero columns
  90.         for (int i = 0; i < length; i++)
  91.         {
  92.             int counter = 0;
  93.             for (int j = 0; j < 8; j++)
  94.             {
  95.                 if (matrix[j, i] == 1)
  96.                 {
  97.                     counter++;
  98.                 }
  99.             }
  100.             if (counter == 0)
  101.             {
  102.                 countOfZeroColumns++;
  103.             }
  104.         }
  105.         return sum * countOfZeroColumns;
  106.     }
  107.     static void Main(string[] args)
  108.     {
  109.         //fill the matrix
  110.         int n = int.Parse(Console.ReadLine());
  111.         int[,] matrix = new int[8, n];
  112.         for (int i = 0; i < 8; i++)
  113.         {
  114.             int number = int.Parse(Console.ReadLine());
  115.             int ctr = n - 1;
  116.             for (int j = 0; j < n; j++)
  117.             {
  118.                 matrix[i, j] = (number >> ctr) & 1;
  119.                 ctr--;
  120.             }
  121.         }
  122.         bool finished = false;
  123.         while (true)
  124.         {
  125.             string word = Console.ReadLine();
  126.             switch (word)
  127.             {
  128.                 case "reset":
  129.                     {
  130.                         matrix = Reset(matrix, n);
  131.                         break;
  132.                     }
  133.                 case "right":
  134.                     {
  135.                         int row = int.Parse(Console.ReadLine());
  136.                         if (row > 7) row = 7;
  137.                         if (row < 0) row = 0;
  138.                         int col = int.Parse(Console.ReadLine());
  139.                         if (col > n - 1) col = n - 1;
  140.                         if (col < 0) col = 0;
  141.                         matrix = MoveRight(matrix, row, col, n);
  142.                         break;
  143.                     }
  144.                 case "left":
  145.                     {
  146.                         int row = int.Parse(Console.ReadLine());
  147.                         if (row > 7) row = 7;
  148.                         if (row < 0) row = 0;
  149.                         int col = int.Parse(Console.ReadLine());
  150.                         if (col > n - 1) col = n - 1;
  151.                         if (col < 0) col = 0;
  152.                         matrix = MoveLeft(matrix, row, col);
  153.                         break;
  154.                     }
  155.                 case "stop":
  156.                     {
  157.                         Console.WriteLine(Count(matrix, n));
  158.                         finished = true;
  159.                         break;
  160.                     }
  161.             }
  162.             if (finished)
  163.             {
  164.                 break;
  165.             }
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement