sashomaga

Bombing Cuboids

Feb 2nd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static Dictionary<char, int> blowLetters = new Dictionary<char, int>();
  8.     static int count;
  9.  
  10.     static void Main()
  11.     {
  12.         int width; // x
  13.         int height; // y
  14.         int depth; // z
  15.  
  16.         string[] input = Console.ReadLine().Split();
  17.         width = int.Parse(input[0]);
  18.         height = int.Parse(input[1]);
  19.         depth = int.Parse(input[2]);
  20.  
  21.         char[, ,] cube = new char[width, height, depth];
  22.         //bool[, ,] visited = new bool[width, height, depth];
  23.  
  24.         string[] rows = new string[height]; // y
  25.         string[] columns = new string[depth]; // z
  26.         string cells = string.Empty; // x
  27.         for (int y = 0; y < height; y++)
  28.         {
  29.             rows[y] = Console.ReadLine();
  30.             //rows[0] = "AAAA AAAA AAAA AAAA AAAA";
  31.             //rows[1] = "AAAA AAAA AAAA AAAA AAAC";
  32.             //rows[2] = "ABAA AAAA AAAA AAAA AAAA";
  33.             for (int z = 0; z < depth; z++)
  34.             {
  35.                 columns = rows[y].Split(new string[] { " " }, StringSplitOptions.None);
  36.                 cells = columns[z];
  37.                 for (int x = 0; x < width; x++)
  38.                 {
  39.                     cube[x, y, z] = cells[x];
  40.                 }
  41.  
  42.             }
  43.         }
  44.         List<Bombs> bomb = new List<Bombs>();
  45.         int numOfBombs = int.Parse(Console.ReadLine());
  46.         string[] bombDetails = new string[numOfBombs];
  47.         for (int i = 0; i < numOfBombs; i++)
  48.         {
  49.             bombDetails = Console.ReadLine().Split();
  50.             bomb.Add(new Bombs(int.Parse(bombDetails[0]), int.Parse(bombDetails[1]), int.Parse(bombDetails[2]), int.Parse(bombDetails[3])));
  51.         }
  52.  
  53.         for (int i = 0; i < bomb.Count; i++)
  54.         {
  55.             BlowBomb(bomb[i], cube, width, height, depth);
  56.             FallDown(bomb[i], cube, width, height, depth);
  57.         }
  58.        
  59.         //PrintCube(cube);
  60.         Console.WriteLine(count);
  61.         var sorted = blowLetters.OrderBy(x => x.Key);
  62.         foreach (var item in sorted)
  63.         {
  64.             Console.WriteLine(item.Key + " " + item.Value);
  65.         }
  66.  
  67.     }
  68.  
  69.     private static void FallDown(Bombs currentBomb, char[, ,] cube,int width,int height,int depth)
  70.     {
  71.         //current bomb height
  72.         Point currentPoint = new Point(currentBomb.x, currentBomb.y, currentBomb.z);
  73.         FallDownColumn(currentPoint, cube);
  74.  
  75.         int startDepth = Math.Max(currentBomb.z - currentBomb.power, 0);
  76.         int endDepth = Math.Min(currentBomb.z + currentBomb.power + 1, depth);
  77.         int startWidth = Math.Max(currentBomb.x - currentBomb.power, 0);
  78.         int endWidth = Math.Min(currentBomb.x + currentBomb.power + 1, width);
  79.  
  80.         for (int z = startDepth; z < endDepth; z++)
  81.         {
  82.  
  83.             for (int x = startWidth; x < endWidth; x++)
  84.             {
  85.                 currentPoint.x = x;
  86.                 currentPoint.z = z;
  87.                 FallDownColumn(currentPoint, cube);
  88.             }
  89.  
  90.         }
  91.  
  92.  
  93.         ////x +
  94.         //for (int i = currentBomb.x + 1; i < cube.GetLength(0) && i <= (currentBomb.x + currentBomb.power + 1); i++)
  95.         //{
  96.         //    currentPoint.z = currentBomb.z;
  97.         //    currentPoint.x = i;
  98.         //    FallDownColumn(currentPoint, cube);
  99.         //}
  100.         //// x-
  101.         //for (int i = currentBomb.x - 1; i >= 0 && i >= (currentBomb.x - currentBomb.power - 1); i--)
  102.         //{
  103.         //    currentPoint.z = currentBomb.z;
  104.         //    currentPoint.x = i;
  105.         //    FallDownColumn(currentPoint, cube);
  106.         //}
  107.         //// z+
  108.         //for (int i = currentBomb.z + 1; i < cube.GetLength(2) && i <= (currentBomb.z + currentBomb.power + 1); i++)
  109.         //{
  110.         //    currentPoint.x = currentBomb.x;
  111.         //    currentPoint.z = i;
  112.         //    FallDownColumn(currentPoint, cube);
  113.         //}
  114.         //// z-
  115.         //for (int i = currentBomb.z - 1; i >= 0 && i >= (currentBomb.z - currentBomb.power - 1); i--)
  116.         //{
  117.         //    currentPoint.x = currentBomb.x;
  118.         //    currentPoint.z = i;
  119.         //    FallDownColumn(currentPoint, cube);
  120.         //}
  121.  
  122.     }
  123.  
  124.     private static void FallDownColumn(Point currentPoint, char[, ,] cube)
  125.     {
  126.  
  127.         for (int pos = 0; pos < cube.GetLength(1); pos++)
  128.         {
  129.             if (cube[currentPoint.x, pos, currentPoint.z] == '*')
  130.             {
  131.                 Point empty = new Point(currentPoint.x, pos, currentPoint.z);
  132.                 Swap(empty, cube);
  133.             }
  134.         }
  135.     }
  136.  
  137.     private static void Swap(Point empty, char[, ,] cube)
  138.     {
  139.         for (int pos = empty.y + 1; pos < cube.GetLength(1); pos++)
  140.         {
  141.             if (cube[empty.x, pos, empty.z] != '*')
  142.             {
  143.                 cube[empty.x, empty.y, empty.z] = cube[empty.x, pos, empty.z];
  144.                 cube[empty.x, pos, empty.z] = '*';
  145.                 return;
  146.             }
  147.         }
  148.     }
  149.  
  150.     private static void BlowBomb(Bombs currentBomb, char[, ,] cube, int width, int height, int depth)
  151.     {
  152.  
  153.         Point currentPoint = new Point(currentBomb.x, currentBomb.y, currentBomb.z);
  154.         BlowCube(cube, currentPoint);
  155.  
  156.         int startDepth = Math.Max(currentBomb.z - currentBomb.power, 0);
  157.         int endDepth = Math.Min(currentBomb.z + currentBomb.power + 1, depth);
  158.         int startHeight = Math.Max(currentBomb.y - currentBomb.power, 0);
  159.         int endHeight = Math.Min(currentBomb.y + currentBomb.power + 1, height);
  160.         int startWidth = Math.Max(currentBomb.x - currentBomb.power, 0);
  161.         int endWidth = Math.Min(currentBomb.x + currentBomb.power + 1, width);
  162.  
  163.         for (int y = startHeight; y < endHeight; y++)
  164.         {
  165.  
  166.             for (int z = startDepth; z < endDepth; z++)
  167.             {
  168.  
  169.                 for (int x = startWidth; x < endWidth; x++)
  170.                 {
  171.                     currentPoint.x = x;
  172.                     currentPoint.y = y;
  173.                     currentPoint.z = z;
  174.                     if (CheckDistance(currentPoint, currentBomb))
  175.                     {
  176.                         BlowCube(cube, currentPoint);
  177.                     }
  178.                 }
  179.  
  180.             }
  181.         }
  182.        
  183.     // x +        
  184.     //    for (int x = currentBomb.x + 1 ; x < cube.GetLength(0) && x <= (currentBomb.x + currentBomb.power); x++)
  185.     //    {
  186.     //        currentPoint.x = x;
  187.     //        currentPoint.y = currentBomb.y;
  188.     //        currentPoint.z = currentBomb.z;
  189.     //        if (CheckDistance(currentPoint, currentBomb))
  190.     //        {
  191.     //            BlowCube(cube, currentPoint);
  192.     //        }
  193.     //    }
  194.     //    // x -        
  195.     //    for (int x = currentBomb.x -1 ; x >= 0 && x >= (currentBomb.x - currentBomb.power); x--)
  196.     //    {
  197.     //        currentPoint.x = x;
  198.     //        currentPoint.y = currentBomb.y;
  199.     //        currentPoint.z = currentBomb.z;
  200.     //        if (CheckDistance(currentPoint, currentBomb))
  201.     //        {
  202.     //            BlowCube(cube, currentPoint);
  203.     //        }
  204.     //    }
  205.     //    // y +        
  206.     //    for (int y = currentBomb.y +1 ; y < cube.GetLength(1) && y <= (currentBomb.y + currentBomb.power); y++)
  207.     //    {
  208.     //        currentPoint.x = currentBomb.x;
  209.     //        currentPoint.y = y;
  210.     //        currentPoint.z = currentBomb.z;
  211.     //        if (CheckDistance(currentPoint, currentBomb))
  212.     //        {
  213.     //            BlowCube(cube, currentPoint);
  214.     //        }
  215.     //    }
  216.     //    // y -        
  217.     //    for (int y = currentBomb.y -1 ; y >= 0 && y >= (currentBomb.y - currentBomb.power); y--)
  218.     //    {
  219.     //        currentPoint.x = currentBomb.x;
  220.     //        currentPoint.y = y;
  221.     //        currentPoint.z = currentBomb.z;
  222.     //        if (CheckDistance(currentPoint, currentBomb))
  223.     //        {
  224.     //            BlowCube(cube, currentPoint);
  225.     //        }
  226.     //    }
  227.     //    // z +        
  228.     //    for (int z = currentBomb.z +1; z < cube.GetLength(2) && z <= (currentBomb.z + currentBomb.power); z++)
  229.     //    {
  230.     //        currentPoint.x = currentBomb.x;
  231.     //        currentPoint.y = currentBomb.y;
  232.     //        currentPoint.z = z;
  233.     //        if (CheckDistance(currentPoint, currentBomb))
  234.     //        {
  235.     //            BlowCube(cube, currentPoint);
  236.     //        }
  237.     //    }
  238.     //    // z -        
  239.     //    for (int z = currentBomb.z -1; z >= 0 && z >= (currentBomb.z - currentBomb.power); z--)
  240.     //    {
  241.     //        currentPoint.x = currentBomb.x;
  242.     //        currentPoint.y = currentBomb.y;
  243.     //        currentPoint.z = z;
  244.     //        if (CheckDistance(currentPoint, currentBomb))
  245.     //        {
  246.     //            BlowCube(cube, currentPoint);
  247.     //        }
  248.     //    }
  249.     }
  250.  
  251.     private static void BlowCube(char[, ,] cube, Point currentPoint)
  252.     {
  253.         char currentCubeChar = cube[currentPoint.x, currentPoint.y, currentPoint.z];
  254.         if (currentCubeChar == '*')
  255.         {
  256.             return;
  257.         }
  258.         if (blowLetters.ContainsKey(currentCubeChar))
  259.         {
  260.             blowLetters[currentCubeChar]++;
  261.             count++;
  262.         }
  263.         else
  264.         {
  265.             blowLetters.Add(currentCubeChar, 1);
  266.             count++;
  267.         }
  268.         cube[currentPoint.x, currentPoint.y, currentPoint.z] = '*'; //mark
  269.     }
  270.  
  271.     private static bool CheckDistance(Point currentPoint, Bombs currentBomb)
  272.     {
  273.         double result;
  274.         result = (currentPoint.x - currentBomb.x) * (currentPoint.x - currentBomb.x) +
  275.             (currentPoint.y - currentBomb.y) * (currentPoint.y - currentBomb.y) +
  276.             (currentPoint.z - currentBomb.z) * (currentPoint.z - currentBomb.z);
  277.         if (result <= currentBomb.power * currentBomb.power)
  278.         {
  279.             return true; // boom
  280.         }
  281.         else
  282.         {
  283.             return false; // no boom
  284.         }
  285.     }
  286.  
  287.     private static void PrintCube(char[, ,] cube)
  288.     {
  289.         for (int y = 0; y < cube.GetLength(1); y++)
  290.         {
  291.  
  292.             for (int z = 0; z < cube.GetLength(2); z++)
  293.             {
  294.  
  295.                 for (int x = 0; x < cube.GetLength(0); x++)
  296.                 {
  297.  
  298.                     Console.Write(cube[x, y, z]);
  299.                 }
  300.                 Console.Write(" ");
  301.             }
  302.             Console.WriteLine();
  303.         }
  304.     }
  305. }
  306.  
  307. class Bombs
  308. {
  309.     public int x;
  310.     public int y;
  311.     public int z;
  312.     public int power;
  313.  
  314.     public Bombs(int x, int y, int z, int power)
  315.     {
  316.         this.x = x;
  317.         this.y = y;
  318.         this.z = z;
  319.         this.power = power;
  320.     }
  321. }
  322.  
  323. class Point
  324. {
  325.     public int x;
  326.     public int y;
  327.     public int z;
  328.  
  329.     public Point(int x, int y, int z)
  330.     {
  331.         this.x = x;
  332.         this.y = y;
  333.         this.z = z;
  334.     }
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment