Advertisement
g-stoyanov

ExamC# 29.12.2012 Task5

Dec 29th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2.  
  3. class Task5
  4. {
  5.     static void Main()
  6.     {
  7.         int y = 0;
  8.         bool hit = false;
  9.         int flight = 0;
  10.         int killedPigs = 0;
  11.         int result = 0;
  12.         int direction = -1;
  13.         int[,] matrix = new int[8, 16];
  14.         string input = "";
  15.         for (byte x = 0; x < 8; x++)
  16.         {
  17.             input = Convert.ToString(int.Parse(Console.ReadLine()), 2).PadLeft(16, '0');
  18.             for (int a = 0; a < 16; a++)
  19.             {
  20.                 matrix[x, a] = int.Parse(Convert.ToString(input[a]));
  21.             }
  22.         }
  23.  
  24.         for (int i = 7; i >= 0; i--)
  25.         {
  26.             hit = false;
  27.             for (int Y = 0; Y < 8; Y++)
  28.             {
  29.                 if (matrix[Y,i] == 1)
  30.                 {
  31.                     matrix[Y, i] = 0;
  32.                     direction = -1;
  33.                     killedPigs = 0;
  34.                     flight = 0;
  35.                     int h = i;
  36.                     y = Y;
  37.                     while (h < 16)
  38.                     {
  39.                         if (y + direction > 7)
  40.                         {
  41.                             break;
  42.                         }
  43.                         flight++;
  44.                         if (y + direction > 7 || y + direction < 0)
  45.                         {
  46.                             direction = direction * -1;
  47.                         }
  48.                         h++;
  49.                         y = y + direction;
  50.                         if (y > 7 || y < 0 || h > 15 || h < 0)
  51.                         {
  52.                             continue;
  53.                         }
  54.                         if (h > 7)
  55.                         {
  56.                             if (matrix[y, h] == 1)
  57.                             {
  58.                                 hit = true;
  59.                                 for (int z = y - 1; z < y + 2; z++)
  60.                                 {
  61.                                     for (int k = h - 1; k < h + 2; k++)
  62.                                     {
  63.                                         if (z > 7 || z < 0 || k > 15 || k < 0)
  64.                                         {
  65.                                             continue;
  66.                                         }
  67.                                         if (matrix[z, k] == 1)
  68.                                         {
  69.                                             killedPigs++;
  70.                                             matrix[z, k] = 0;
  71.                                         }
  72.                                     }
  73.                                 }
  74.                             }
  75.                         }
  76.                         if (hit)
  77.                         {
  78.                             break;
  79.                         }
  80.                     }
  81.                 }
  82.                 if (hit)
  83.                 {
  84.                     result = result + (flight * killedPigs);
  85.                     break;
  86.                 }
  87.             }
  88.         }
  89.         int check = 0;
  90.         string win = "Yes";
  91.         for (int i = 0; i < 8; i++)
  92.         {
  93.             for (int z = 0; z < 16; z++)
  94.             {
  95.                 check = check + matrix[i, z];
  96.             }
  97.         }
  98.         if (check > 0)
  99.         {
  100.             win = "No";
  101.         }
  102.         Console.WriteLine(result + " " + win);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement