Advertisement
vlad0

3D Stars

Feb 4th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _04._3D_Stars
  7. {
  8.     class Program
  9.     {
  10.         static char[, ,] cuboid;
  11.         static Dictionary<char,int> result = new Dictionary<char,int>();
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             string[] dimensions = Console.ReadLine().Split(' ');
  16.             //string[] dimensions = "7 4 3".Split(' ');
  17.             int width = int.Parse(dimensions[0]);
  18.             int height = int.Parse(dimensions[1]);
  19.             int depth = int.Parse(dimensions[2]);
  20.  
  21.             cuboid = new char[depth, height, width];
  22.             //string[] input = {
  23.             //                    "BRYYYYY RYYYYGY YRYYYYY",
  24.             //                    "YYYGBGY YYYYGGG YYYGGGY",
  25.             //                    "RYBYGYY RYYYYGY RYBYGBB",
  26.             //                    "RYBYGYY RBYYGYY RYBYGBB"
  27.  
  28.             //                  };
  29.             FillCuboid(width, height, depth/*, input*/);
  30.             FindNeighbours();
  31.             Console.WriteLine(result.Sum(x => x.Value));
  32.             foreach (var item in result.OrderBy(x => x.Key))
  33.             {
  34.                 Console.WriteLine("{0} {1}",item.Key, item.Value);
  35.             }
  36.         }
  37.  
  38.         private static void FindNeighbours()
  39.         {
  40.             for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  41.             {
  42.                 for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  43.                 {
  44.                     for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  45.                     {
  46.                         char currentColor = cuboid[slice,rows,cols];
  47.                         int count = 1;
  48.                         //check upper cuboid
  49.                         if (rows - 1 >= 0 && cuboid[slice, rows - 1, cols] == currentColor)
  50.                         {
  51.                             count++;
  52.                         }
  53.                         else continue;
  54.                         //check downer cuboid
  55.                         if (rows + 1 < cuboid.GetLength(1) && cuboid[slice, rows + 1, cols] == currentColor)
  56.                         {
  57.                             count++;
  58.                         }
  59.                         else continue;
  60.                         //check left cuboid
  61.                         if (cols - 1 >= 0 && cuboid[slice, rows, cols-1] == currentColor)
  62.                         {
  63.                             count++;
  64.                         }
  65.                         else continue;
  66.                         //check right cuboid
  67.                         if (cols + 1 < cuboid.GetLength(2) && cuboid[slice, rows, cols+1] == currentColor)
  68.                         {
  69.                             count++;
  70.                         }
  71.                         else continue;
  72.                         //check backward cuboid
  73.                         if (slice - 1 >= 0 && cuboid[slice-1, rows, cols] == currentColor)
  74.                         {
  75.                             count++;
  76.                         }
  77.                         else continue;
  78.                         //check upward cuboid
  79.                         if (slice + 1 < cuboid.GetLength(0) && cuboid[slice+1, rows, cols] == currentColor)
  80.                         {
  81.                             count++;
  82.                         }
  83.                         else continue;
  84.  
  85.                         if (count == 7)
  86.                         {
  87.                             Score(currentColor);
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private static void Score(char currentColor)
  95.         {
  96.             if (result.ContainsKey(currentColor))
  97.             {
  98.                 result[currentColor]++;
  99.             }
  100.             else
  101.             {
  102.                 result.Add(currentColor, 1);
  103.             }
  104.         }
  105.  
  106.         private static void FillCuboid(int width, int height, int depth /*,string[] input*/)
  107.         {
  108.             for (int rows = 0; rows < height; rows++)
  109.             {
  110.                 //string[] line = input[rows].Split(' ');
  111.                 string[] line = Console.ReadLine().Split(' ');
  112.                 int lineLength = line.Length;
  113.  
  114.                 for (int slices = 0; slices < lineLength; slices++)
  115.                 {
  116.                     int slicesLength = line[slices].Length;
  117.  
  118.                     for (int cols = 0; cols < slicesLength; cols++)
  119.                     {
  120.                         cuboid[slices, rows, cols] = line[slices][cols];
  121.                     }
  122.                 }
  123.  
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement