Advertisement
nikolov_k

Bombing Cuboids

Feb 2nd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. struct Bomb
  5. {
  6.     public int w;
  7.     public int h;
  8.     public int d;
  9.     public int p;
  10. }
  11.  
  12. struct Color
  13. {
  14.     public char Name;
  15.     public int Count;
  16. }
  17.  
  18. class BombingCuboids
  19. {
  20.     static char[, ,] cuboid;
  21.     static int width, height, depth;
  22.     static List<Bomb> bombs = new List<Bomb>();
  23.     static List<Color> removedColors = new List<Color>();
  24.     static int totalRemoved= 0;
  25.  
  26.     static void Main()
  27.     {
  28.         InputCuboid();
  29.         InputBombs();
  30.         for (int i = 0; i < bombs.Count; i++)
  31.         {
  32.             MakeBoom(bombs[i]);
  33.         }
  34.         removedColors.Sort((x,y) => x.Name.CompareTo(y.Name));
  35.         Console.WriteLine(totalRemoved);
  36.         foreach (var color in removedColors)
  37.         {
  38.             Console.WriteLine("{0} {1}", color.Name, color.Count);
  39.         }
  40.     }
  41.  
  42.     static void MakeBoom(Bomb bomb)
  43.     {
  44.         //Remove cubs
  45.         for (int w = 0; w < width; w++)
  46.         {
  47.             for (int d = 0; d < depth; d++)
  48.             {
  49.                 int removed = 0;
  50.                 for (int h = 0; h < height; h++)
  51.                 {
  52.                     if (cuboid[w,h,d] == '0')
  53.                     {
  54.                         continue;
  55.                     }
  56.                     double distance = Math.Sqrt((w - bomb.w) * (w - bomb.w) + (h - bomb.h) * (h - bomb.h) + (d - bomb.d) * (d - bomb.d));
  57.                     if (distance <= bomb.p)
  58.                     {
  59.                         int index = removedColors.FindIndex(a => a.Name == cuboid[w,h,d] && a.Count > 0);
  60.                         if (index>=0)
  61.                         {
  62.                             Color color = removedColors[index];
  63.                             color.Count++;
  64.                             removedColors[index] = color;
  65.                         }
  66.                         else
  67.                         {
  68.                             Color newColor = new Color();
  69.                             newColor.Name = cuboid[w, h, d];
  70.                             newColor.Count = 1;
  71.                             removedColors.Add(newColor);
  72.                         }
  73.                         cuboid[w, h, d] = '0';
  74.                         removed++;
  75.                         totalRemoved++;
  76.                     }
  77.                 }
  78.                 if (removed > 0)
  79.                 {
  80.                     FallDown(w, d, removed);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     static void FallDown(int w, int d, int removed)
  87.     {
  88.         int removedStart = 0;
  89.         while (cuboid[w,removedStart,d] != '0')
  90.         {
  91.             removedStart++;
  92.         }
  93.         while (removedStart + removed < height)
  94.         {
  95.             cuboid[w,removedStart,d] = cuboid[w,removedStart+removed,d];
  96.             removedStart++;
  97.         }
  98.         while (removedStart<height)
  99.         {
  100.             cuboid[w, removedStart, d] = '0';
  101.             removedStart++;
  102.         }
  103.     }
  104.  
  105.     static void InputBombs()
  106.     {
  107.         int n = int.Parse(Console.ReadLine());
  108.         for (int i = 0; i < n; i++)
  109.         {
  110.             Bomb current = new Bomb();
  111.             string[] line = Console.ReadLine().Split(' ');
  112.             current.w = int.Parse(line[0]);
  113.             current.h = int.Parse(line[1]);
  114.             current.d = int.Parse(line[2]);
  115.             current.p = int.Parse(line[3]);
  116.             bombs.Add(current);
  117.         }
  118.     }
  119.     static void InputCuboid()
  120.     {
  121.         string[] dimensions = Console.ReadLine().Split(' ');
  122.         width = int.Parse(dimensions[0]);
  123.         height = int.Parse(dimensions[1]);
  124.         depth = int.Parse(dimensions[2]);
  125.         cuboid = new char[width,height,depth];
  126.         for (int h = 0; h < height; h++)
  127.         {
  128.             string[] line = Console.ReadLine().Split(' ');
  129.             for (int d = 0; d < depth; d++)
  130.             {
  131.                 for (int w = 0; w < width; w++)
  132.                 {
  133.                     cuboid[w, h, d] = line[d][w];
  134.                 }
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement