BorislavBorisov

25.Angry Bits мое решение

Oct 12th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. class AngryBits
  3. {
  4.     static int[,] matrix = new int[8, 16];
  5.     static int count = 0;
  6.     static void Main()
  7.     {
  8.  
  9.         for (int row = 0; row < 8; row++)
  10.         {
  11.             int bit = int.Parse(Console.ReadLine());
  12.             for (int col = 15; col >= 0; col--)
  13.             {
  14.                 matrix[row,col] = bit >> col & 1;
  15.             }
  16.         }
  17.         for (int col = 8; col <= 15; col++)
  18.         {
  19.             string direction = "right";
  20.             int currentRow = -1;
  21.             for (int row = 0; row < 8; row++)
  22.             {
  23.                 if (matrix[row, col] == 1)
  24.                 {
  25.                     currentRow = row;
  26.                     matrix[row, col] = 0;
  27.                     break;
  28.                 }
  29.             }
  30.             if (currentRow == -1)
  31.             {
  32.                 continue;
  33.             }
  34.             if (currentRow == 0)
  35.             {
  36.                 direction = "down";
  37.             }
  38.             int currentCount = 0;
  39.             int currentCol = col;
  40.             while (true)
  41.             {
  42.                 if (direction == "right")
  43.                 {
  44.                     currentCol--;
  45.                     currentRow--;
  46.                     if (matrix[currentRow, currentCol] == 1)
  47.                     {
  48.                         currentCount = col - currentCol;
  49.                         CheckForPigs(currentRow, currentCol,currentCount);
  50.                         break;
  51.                     }
  52.                     else if (currentRow == 0)
  53.                     {
  54.                         direction = "down";
  55.                     }
  56.                 }
  57.                 if (direction == "down")
  58.                 {
  59.                     currentCol--;
  60.                     currentRow++;
  61.                     if (matrix[currentRow, currentCol] == 1)
  62.                     {
  63.                         currentCount = col - currentCol;//по формула//Path
  64.                         CheckForPigs(currentRow, currentCol, currentCount);
  65.                         break;
  66.                     }
  67.                     if (currentCol == 0 || currentRow == 7)
  68.                     {
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.        
  75.         bool isPigsHitted = true;
  76.         for (int row = 0; row < 8; row++)
  77.         {
  78.             for (int col = 0; col < 16; col++)
  79.             {
  80.                 if (matrix[row, col] == 1)
  81.                 {
  82.                     isPigsHitted = false;
  83.                     break;
  84.                 }
  85.             }
  86.         }
  87.         if (isPigsHitted)
  88.         {
  89.             Console.WriteLine(count + " Yes");
  90.         }
  91.         else
  92.         {
  93.             Console.WriteLine(count + " No");
  94.         }
  95.     }
  96.  
  97.     static void CheckForPigs(int currentRow, int currentCol,int currentCount)
  98.     {
  99.         int score = 0;
  100.         for (int i = currentRow - 1; i <= currentRow + 1; i++)
  101.         {
  102.             for (int j = currentCol + 1; j >= currentCol - 1; j--)
  103.             {
  104.                 if (i != -1 && i != 8 && (j != -1))
  105.                 {
  106.                     if (matrix[i, j] == 1)
  107.                     {
  108.                         score++;
  109.                         matrix[i, j] = 0;
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         count += score * currentCount;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment