Advertisement
vic_alexiev

AngryBits.cs

Dec 31st, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System;
  2.  
  3. class AngryBits
  4. {
  5.     private static int n = 8;
  6.     private static int[,] field = new int[n, 2 * n];
  7.  
  8.     private static int GetKilledPigs(int row, int col)
  9.     {
  10.         int pigsCount = 0;
  11.         int startRow = Math.Max(0, row - 1);
  12.         int endRow = Math.Min(n - 1, row + 1);
  13.         int startCol = col + 1;
  14.         int endCol = Math.Max(0, col - 1);
  15.  
  16.         // find the pigs in the 3x3 neighbourhood
  17.         for (int i = startRow; i <= endRow; i++)
  18.         {
  19.             for (int j = startCol; j >= endCol; j--)
  20.             {
  21.                 if (field[i, j] == 1)
  22.                 {
  23.                     pigsCount++;
  24.                     // clear the bit
  25.                     field[i, j] = 0;
  26.                 }
  27.             }
  28.         }
  29.  
  30.         return pigsCount;
  31.     }
  32.  
  33.     /// <summary>
  34.     /// Calculates the score of the bird located in field[row, col] -
  35.     /// the length of the flight multiplied by the number of pigs killed.
  36.     /// </summary>
  37.     /// <param name="row"></param>
  38.     /// <param name="col"></param>
  39.     /// <returns></returns>
  40.     private static int ThrowBird(int row, int col)
  41.     {
  42.         // clear the bit of the bird
  43.         field[row, col] = 0;
  44.  
  45.         bool flightFinished = false;
  46.         int flight = 0;
  47.         int pigsKilled = 0;
  48.         int upFlight = row;
  49.         int downFlight = Math.Min(n - 1, col - row);
  50.  
  51.         // fly upwards
  52.         for (int i = 0; i < upFlight; i++)
  53.         {
  54.             flight++;
  55.             // the bird has hit a pig
  56.             if (field[--row, --col] == 1)
  57.             {
  58.                 pigsKilled = GetKilledPigs(row, col);
  59.                 flightFinished = true;
  60.                 break;
  61.             }
  62.         }
  63.  
  64.         // fly downwards
  65.         if (!flightFinished)
  66.         {
  67.             for (int i = 0; i < downFlight; i++)
  68.             {
  69.                 flight++;
  70.                 // the bird has hit a pig
  71.                 if (field[++row, --col] == 1)
  72.                 {
  73.                     pigsKilled = GetKilledPigs(row, col);
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         return flight * pigsKilled;
  80.     }
  81.  
  82.     /// <summary>
  83.     /// Checks if there are remaining pigs on the right side
  84.     /// of the field.
  85.     /// </summary>
  86.     /// <returns></returns>
  87.     private static bool PlayerWins()
  88.     {
  89.         for (int i = 0; i < n; i++)
  90.         {
  91.             for (int j = 0; j < n; j++)
  92.             {
  93.                 if (field[i, j] == 1)
  94.                 {
  95.                     return false;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         return true;
  101.     }
  102.  
  103.     static void Main()
  104.     {
  105.         for (int i = 0; i < n; i++)
  106.         {
  107.             string numberAsString = Console.ReadLine();
  108.             int number = Int32.Parse(numberAsString);
  109.  
  110.             // fill the field with zeros and ones
  111.             for (int j = 0; j < 2 * n; j++)
  112.             {
  113.                 field[i, j] = (number >> j) & 1;
  114.             }
  115.         }
  116.  
  117.         int total = 0;
  118.  
  119.         // start looking for birds column by column
  120.         for (int col = n; col < 2 * n; col++)
  121.         {
  122.             for (int row = 0; row < n; row++)
  123.             {
  124.                 if (field[row, col] == 1)
  125.                 {
  126.                     // this is a bird - throw it at the pigs
  127.                     total += ThrowBird(row, col);
  128.                     // one bird per column
  129.                     break;
  130.                 }
  131.             }
  132.         }
  133.  
  134.         Console.WriteLine("{0} {1}", total, PlayerWins() ? "Yes" : "No");
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement