archangelmihail

AngryBits

Dec 3rd, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 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. namespace AngryBits
  8. {
  9.     class AngryBits
  10.     {
  11.         static int score = 0;
  12.         static int[,] matrix = new int[8, 16];
  13.  
  14.         static void Main()
  15.         {
  16.             //input
  17.             for (int row = 0; row < 8; row++)
  18.             {
  19.                 int number = int.Parse(Console.ReadLine());
  20.                 string numberToString = Convert.ToString(number, 2).PadLeft(16, '0');
  21.                 for (int col = 0; col < 16; col++)
  22.                 {
  23.                     matrix[row, col] = int.Parse(numberToString[col].ToString());
  24.                 }
  25.             }
  26.             // second way for input initialization
  27.             //for (int row = 0; row < 8; row++)
  28.             //{
  29.             //    int number = int.Parse(Console.ReadLine());
  30.             //    for (int col = 0; col < 16; col++)
  31.             //    {
  32.             //        matrix[row, col] = (number >> 15 - col) & 1;
  33.             //    }
  34.             //}
  35.  
  36.             //solution
  37.             for (int col = 7; col >= 0; col--)
  38.             {
  39.                 for (int row = 0; row < 8; row++)
  40.                 {
  41.                     if (matrix[row, col] == 1)
  42.                     {
  43.                         matrix[row, col] = 0;
  44.                         int currentCol = col;
  45.                         int currentRow = row;
  46.                         int path = 0;
  47.                         bool hit=false;
  48.                         // fly up
  49.                         while (currentRow > 0)
  50.                         {
  51.                             path++;
  52.                             currentRow--;
  53.                             currentCol++;
  54.                             if (matrix[currentRow, currentCol] == 1)
  55.                             {
  56.                                 DestroyPigs(currentRow, currentCol, path);
  57.                                 //matrix[currentRow, currentCol] = 0;
  58.                                 hit = true;
  59.                                 break;
  60.                             }
  61.                         }
  62.                         // fly down
  63.                         if (!hit)
  64.                         {
  65.                             while (currentRow < 7 && currentCol < 15 )
  66.                             {
  67.                                 path++;
  68.                                 currentRow++;
  69.                                 currentCol++;
  70.  
  71.                                 if (matrix[currentRow, currentCol] == 1)
  72.                                 {
  73.                                     DestroyPigs(currentRow, currentCol, path);
  74.                                     //matrix[currentRow, currentCol] = 0;
  75.                                     break;
  76.                                 }
  77.  
  78.                                 else if (currentRow == 7 || currentCol == 15)
  79.                                 {
  80.                                     if (matrix[currentRow, currentCol] == 1)
  81.                                     {
  82.                                         DestroyPigs(currentRow, currentCol, path);
  83.                                         //matrix[currentRow, currentCol] = 0;
  84.                                         break;
  85.                                     }
  86.                                    
  87.                                 }
  88.                             }
  89.                         }
  90.                        
  91.                     }
  92.                 }
  93.             }
  94.             //output
  95.             bool win = true;
  96.             for (int row = 0; row < 8; row++)
  97.             {
  98.                 for (int col = 8; col < 16; col++)
  99.                 {
  100.                     if (matrix[row, col] == 1)
  101.                     {
  102.                         win = false;
  103.                     }
  104.                 }
  105.             }
  106.             if (win)
  107.             {
  108.                 Console.WriteLine(score + " Yes");
  109.             }
  110.             else
  111.             {
  112.                 Console.WriteLine(score + " No");
  113.             }
  114.         }
  115.  
  116.         private static void DestroyPigs(int row, int col, int path)
  117.         {
  118.             int tempScore = 0;
  119.             for (int i = row - 1; i <= row + 1; i++)
  120.             {
  121.                 for (int j = col - 1; j <= col + 1; j++)
  122.                 {
  123.                     if (j > 7 && j < 16 && i >= 0 && i < 8)
  124.                     {
  125.                         if (matrix[i, j] == 1)
  126.                         {
  127.                             matrix[i, j] = 0;
  128.                             tempScore++;
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.             score += path * tempScore;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment