CuST0M1z3

AngryBits

May 29th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.20 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.  
  8. class AngryBits
  9. {
  10.     static void Main()
  11.     {
  12.         ushort[,] m = new ushort[8, 16];
  13.  
  14.  
  15.         for (int row = 0; row < 8; row++)
  16.         {
  17.             ushort bits = ushort.Parse(Console.ReadLine());
  18.             for (int col = 0; col < 16; col++)
  19.             {
  20.                 if ((bits & (1 << col)) != 0)
  21.                 {
  22.                     m[row, col] = 1;
  23.                 }
  24.                
  25.             }
  26.         }
  27.         ushort pigCount = 0;
  28.         ushort finalResult = 0;
  29.         ushort result = 0;
  30.         ushort count = 0;
  31.         int startRow = 0;
  32.         int startCol = 0;
  33.  
  34.         int pig = 0;
  35.         for (int pigRow = 0; pigRow < 8; pigRow++)
  36.         {
  37.             for (int pigCol = 0; pigCol < 8; pigCol++)
  38.             {
  39.                 if (m[pigRow, pigCol] == 1)
  40.                 {
  41.                     pig++;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         for (int col = 8; col < 16; col++)
  47.         {
  48.             count = 0;
  49.             result = 0;
  50.             pigCount = 0;
  51.             for (int row = 0; row < 8; row++)
  52.             {
  53.                 if (m[row, col] == 1)
  54.                 {
  55.                     startRow = row;
  56.                     startCol = col;
  57.                     m[row,col] = 0;
  58.                     if (row > 0)
  59.                     {
  60.                         row--;
  61.                         col--;
  62.                         while (row > 0 && m[row, col] == 0)
  63.                         {
  64.                             count++;
  65.                             row--;
  66.                             col--;
  67.                         }
  68.                         if (row == 0 && m[row, col] == 0)
  69.                         {
  70.                             count++;
  71.                         }
  72.                         if (m[row, col] == 1)
  73.                         {
  74.                             count++;
  75.                             pigCount++;
  76.                             m[row, col] = 0;
  77.                             if (m[row + 1, col] == 1)
  78.                             {
  79.                                 pigCount++;
  80.                                 m[row + 1, col] = 0;
  81.                             }
  82.                             if (row > 0 && m[row - 1, col] == 1)
  83.                             {
  84.                                 pigCount++;
  85.                                 m[row - 1, col] = 0;
  86.                             }
  87.                             if (m[row + 1, col + 1] == 1)
  88.                             {
  89.                                 pigCount++;
  90.                                 m[row + 1, col + 1] = 0;
  91.                             }
  92.                             if (m[row + 1, col - 1] == 1)
  93.                             {
  94.                                 pigCount++;
  95.                                 m[row + 1, col - 1] = 0;
  96.                             }
  97.                             if (row > 0 && m[row - 1, col + 1] == 1)
  98.                             {
  99.                                 pigCount++;
  100.                                 m[row - 1, col + 1] = 0;
  101.                             }
  102.                             if (row > 0 && m[row - 1, col - 1] == 1)
  103.                             {
  104.                                 pigCount++;
  105.                                 m[row - 1, col - 1] = 0;
  106.                             }
  107.                             if (m[row, col + 1] == 1)
  108.                             {
  109.                                 pigCount++;
  110.                                 m[row, col + 1] = 0;
  111.                             }
  112.                             if (m[row, col - 1] == 1)
  113.                             {
  114.                                 pigCount++;
  115.                                 m[row, col - 1] = 0;
  116.                             }
  117.                             result += (ushort)(count * pigCount);
  118.                             pig -= pigCount;
  119.                             finalResult += result;
  120.                         }
  121.                     }
  122.                     if (result > 0)
  123.                     {
  124.                         col = startCol;
  125.                         break;
  126.                     }
  127.                     if (row == 0)
  128.                     {
  129.                         row++;
  130.                         col--;
  131.                         while (row < 7 && col > 0 && m[row,col] == 0)
  132.                         {
  133.                             count++;
  134.                             row++;
  135.                             col--;
  136.                         }
  137.                         if (((row == 7) || (col == 0)) && m[row, col] == 0)
  138.                         {
  139.                             count = 0;
  140.                         }
  141.                         if (m[row, col] == 1)
  142.                         {
  143.                             count++;
  144.                             pigCount++;
  145.                             m[row, col] = 0;
  146.                             if (row < 7 && m[row + 1, col] == 1)
  147.                             {
  148.                                 pigCount++;
  149.                                 m[row + 1, col] = 0;
  150.                             }
  151.                             if (m[row - 1, col] == 1)
  152.                             {
  153.                                 pigCount++;
  154.                                 m[row - 1, col] = 0;
  155.                             }
  156.                             if (row < 7 && m[row + 1, col + 1] == 1)
  157.                             {
  158.                                 pigCount++;
  159.                                 m[row + 1, col + 1] = 0;
  160.                             }
  161.                             if (row < 7 && col > 0 && m[row + 1, col - 1] == 1)
  162.                             {
  163.                                 pigCount++;
  164.                                 m[row + 1, col - 1] = 0;
  165.                             }
  166.                             if (m[row - 1, col + 1] == 1)
  167.                             {
  168.                                 pigCount++;
  169.                                 m[row - 1, col + 1] = 0;
  170.                             }
  171.                             if (col > 0 && m[row - 1, col - 1] == 1)
  172.                             {
  173.                                 pigCount++;
  174.                                 m[row - 1, col - 1] = 0;
  175.                             }
  176.                             if (m[row, col + 1] == 1)
  177.                             {
  178.                                 pigCount++;
  179.                                 m[row, col + 1] = 0;
  180.                             }
  181.                             if (col > 0 && m[row, col - 1] == 1)
  182.                             {
  183.                                 pigCount++;
  184.                                 m[row, col - 1] = 0;
  185.                             }
  186.                             result += (ushort)(count * pigCount);
  187.                             pig -= pigCount;
  188.                             finalResult += result;
  189.                         }
  190.                     }
  191.                     col = startCol;
  192.                     break;
  193.                 }                
  194.             }
  195.         }
  196.         if (pig == 0)
  197.         {
  198.             Console.WriteLine(finalResult + " " + "Yes");
  199.         }
  200.         else
  201.         {
  202.             Console.WriteLine(finalResult + " " + "No");
  203.         }
  204.        
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment