Advertisement
stanevplamen

02.09.05.04.3DStars

Jul 12th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Stars
  5. {
  6.     static void Main()
  7.     {
  8.         string inputCubeStr = Console.ReadLine();  // "7 4 3";          
  9.         string[] parcedDimensions = inputCubeStr.Split();
  10.         int cubeWidth = int.Parse(parcedDimensions[0]);
  11.         int cubeHeigth = int.Parse(parcedDimensions[1]);
  12.         int cubeDepth = int.Parse(parcedDimensions[2]);
  13.  
  14.         string[] textCubeLines = new string[cubeHeigth];
  15.  
  16.         for (int i = 0; i < textCubeLines.Length; i++)
  17.         {
  18.             textCubeLines[i] = Console.ReadLine();
  19.         }
  20.  
  21.         //textCubeLines[0] = "BRYYYYY RYYYYGY YRYYYYY";
  22.         //textCubeLines[1] = "YYYGBGY YYYYGGG YYYGGGY";
  23.         //textCubeLines[2] = "RYBYGYY RYYYYGY RYBYGBB";
  24.         //textCubeLines[3] = "RYBYGYY RBYYGYY RYBYGBB";
  25.  
  26.         char[, ,] generalCube = WriteCubeColors(textCubeLines, cubeWidth, cubeHeigth, cubeDepth);
  27.  
  28.         ChechNumberOfStars(generalCube);
  29.     }
  30.  
  31.     static bool InRange(int w, int h, int d, char[, ,] generalCube)
  32.     {
  33.         bool weigthInRange = w - 1 >= 0 && w + 1 < generalCube.GetLength(0);
  34.         bool heithInRange = h - 1 >= 0 && h + 1 < generalCube.GetLength(1);
  35.         bool depthInRange = d - 1 >= 0 && d + 1 < generalCube.GetLength(2);
  36.  
  37.         return weigthInRange && heithInRange && depthInRange;
  38.     }
  39.  
  40.  
  41.     static bool CheckweHaveStar(int w, int h, int d, bool inRange, char[, ,] generalCube)
  42.     {
  43.         if (inRange == true)
  44.         {
  45.             bool weHaveStar = false;
  46.             char c = generalCube[w, h, d];
  47.             if (c == generalCube[w - 1, h, d] && c == generalCube[w + 1, h, d] && c == generalCube[w, h-1, d]
  48.                 && c == generalCube[w, h + 1, d] && c == generalCube[w, h, d - 1] && c == generalCube[w, h, d + 1])
  49.             {
  50.                 weHaveStar = true;
  51.             }
  52.             return weHaveStar;
  53.         }
  54.         else
  55.         {
  56.             return false;
  57.         }
  58.     }
  59.  
  60.  
  61.     private static void ChechNumberOfStars(char[, ,] generalCube)
  62.     {
  63.         SortedDictionary<char, int> colorsDict = new SortedDictionary<char, int>();
  64.         int counterStars = 0;
  65.         for (int w = 0; w < generalCube.GetLength(0); w++)
  66.         {
  67.             for (int h = 0; h < generalCube.GetLength(1); h++)
  68.             {
  69.                 for (int d = 0; d < generalCube.GetLength(2); d++)
  70.                 {
  71.                     int temp = 0;
  72.                     bool inRange = InRange(w, h, d, generalCube);
  73.                     bool weHaveStar = CheckweHaveStar(w, h, d, inRange, generalCube);
  74.  
  75.                     if (weHaveStar == true)
  76.                     {
  77.                         counterStars++;
  78.                         bool exists = false;
  79.                         foreach (KeyValuePair<char, int> kvp in colorsDict)
  80.                         {
  81.                             if (kvp.Key == generalCube[w, h, d])
  82.                             {
  83.                                 exists = true;
  84.                                 temp = kvp.Value + 1;
  85.                             }
  86.                         }
  87.                         if (exists == false)
  88.                         {
  89.                             colorsDict.Add(generalCube[w, h, d], 1);
  90.                         }
  91.                         else if (exists == true)
  92.                         {
  93.                             colorsDict[generalCube[w, h, d]] = temp;
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.  
  100.         Print(counterStars, colorsDict);
  101.     }
  102.  
  103.     private static void Print(int counterStars, SortedDictionary<char, int> colorsDict)
  104.     {
  105.         Console.WriteLine(counterStars);
  106.         foreach (KeyValuePair<char, int> kvp in colorsDict)
  107.         {
  108.             Console.WriteLine("{0} {1}", kvp.Key, kvp.Value);
  109.         }                                                
  110.     }
  111.  
  112.     private static char[, ,] WriteCubeColors(string[] textCubeLines, int cubeWidth, int cubeHeigth, int cubeDepth)
  113.     {
  114.         char[, ,] generalCube = new char[cubeWidth, cubeHeigth, cubeDepth];
  115.  
  116.         for (int h = 0; h < cubeHeigth; h++)
  117.         {
  118.             string[] currentRow = textCubeLines[h].Split();
  119.             for (int d = 0; d < cubeDepth; d++)
  120.             {
  121.                 char[] currentChars = currentRow[d].ToCharArray();
  122.                 for (int w = 0; w < cubeWidth; w++)
  123.                 {
  124.                     generalCube[w, h, d] = currentChars[w];
  125.                 }
  126.             }
  127.         }
  128.         return generalCube;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement