Advertisement
stanevplamen

02.09.04.04.3DSlices

Jul 12th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using System;
  2.  
  3. class Slices
  4. {
  5.     static void Main()
  6.     {
  7.         string inputMatrixString = Console.ReadLine();  // "2 2 2"; // Console.ReadLine();
  8.         string[] inputAsArray = inputMatrixString.Split();
  9.         int matrixWidth = int.Parse(inputAsArray[0]);
  10.         int matrixHeigth = int.Parse(inputAsArray[1]);
  11.         int matrixDepth = int.Parse(inputAsArray[2]);
  12.  
  13.         string[] tempLines = new string[matrixHeigth];
  14.         //tempLines[0] = "1 2 | 3 4"; //
  15.         //tempLines[1] = "5 6 | 7 8"; //
  16.  
  17.         for (int i = 0; i < matrixHeigth; i++)
  18.         {
  19.             tempLines[i] = Console.ReadLine();
  20.         }
  21.  
  22.         int[,,] matrix = ReadTheInput(matrixWidth, matrixHeigth, matrixDepth, tempLines);
  23.  
  24.         int equalSumsW = CountingEqualSumsW(matrix);
  25.         int equalSumsH = CountingEqualSumsH(matrix);
  26.         int equalSumsD = CountingEqualSumsD(matrix);
  27.  
  28.         Console.WriteLine(equalSumsW + equalSumsH + equalSumsD);
  29.     }
  30.  
  31.     private static int CountingEqualSumsD(int[, ,] matrix)
  32.     {
  33.         int sumOne = 0;
  34.         int sumTwo = 0;
  35.         int countEqualSums = 0;
  36.         for (int i = 0; i < matrix.GetLength(2); i++)
  37.         {
  38.             for (int j = 0; j < i + 1; j++)
  39.             {
  40.                 if (j == matrix.GetLength(2) - 1)
  41.                 {
  42.                     sumOne = int.MinValue;
  43.                     break;
  44.                 }
  45.                 for (int h = 0; h < matrix.GetLength(0); h++)
  46.                 {
  47.                     for (int d = 0; d < matrix.GetLength(1); d++)
  48.                     {
  49.                         sumOne = sumOne + matrix[h, d, j];
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             for (int j = i + 1; j < matrix.GetLength(2); j++)
  55.             {
  56.                 for (int h = 0; h < matrix.GetLength(0); h++)
  57.                 {
  58.                     for (int d = 0; d < matrix.GetLength(1); d++)
  59.                     {
  60.                         sumTwo = sumTwo + matrix[h, d, j];
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             if (sumOne == sumTwo)
  66.             {
  67.                 countEqualSums++;
  68.                 sumTwo = 0;
  69.                 sumOne = 0;
  70.             }
  71.             else
  72.             {
  73.                 sumTwo = 0;
  74.                 sumOne = 0;
  75.                 continue;
  76.             }
  77.         }
  78.         return countEqualSums;
  79.     }
  80.  
  81.     private static int CountingEqualSumsH(int[, ,] matrix)
  82.     {
  83.         int sumOne = 0;
  84.         int sumTwo = 0;
  85.         int countEqualSums = 0;
  86.         for (int i = 0; i < matrix.GetLength(1); i++)
  87.         {
  88.             for (int j = 0; j < i + 1; j++)
  89.             {
  90.                 if (j == matrix.GetLength(1) - 1)
  91.                 {
  92.                     sumOne = int.MinValue;
  93.                     break;
  94.                 }
  95.                 for (int h = 0; h < matrix.GetLength(0); h++)
  96.                 {
  97.                     for (int d = 0; d < matrix.GetLength(2); d++)
  98.                     {
  99.                         sumOne = sumOne + matrix[h, j, d];
  100.                     }
  101.                 }
  102.             }
  103.  
  104.             for (int j = i + 1; j < matrix.GetLength(1); j++)
  105.             {
  106.                 for (int h = 0; h < matrix.GetLength(0); h++)
  107.                 {
  108.                     for (int d = 0; d < matrix.GetLength(2); d++)
  109.                     {
  110.                         sumTwo = sumTwo + matrix[h, j, d];
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             if (sumOne == sumTwo)
  116.             {
  117.                 countEqualSums++;
  118.                 sumTwo = 0;
  119.                 sumOne = 0;
  120.             }
  121.             else
  122.             {
  123.                 sumTwo = 0;
  124.                 sumOne = 0;
  125.                 continue;
  126.             }
  127.         }
  128.         return countEqualSums;
  129.     }
  130.  
  131.     private static int CountingEqualSumsW(int[, ,] matrix)
  132.     {
  133.         int sumOne = 0;
  134.         int sumTwo = 0;
  135.         int countEqualSums = 0;
  136.         for (int i = 0; i < matrix.GetLength(0); i++)
  137.         {
  138.             for (int j = 0; j < i + 1; j++)
  139.             {
  140.                 if (j == matrix.GetLength(0) - 1)
  141.                 {
  142.                     sumOne = int.MinValue;
  143.                     break;
  144.                 }
  145.                 for (int h = 0; h < matrix.GetLength(1); h++)
  146.                 {
  147.                     for (int d = 0; d < matrix.GetLength(2); d++)
  148.                     {
  149.                         sumOne = sumOne + matrix[j, h, d];
  150.                     }
  151.                 }
  152.             }
  153.  
  154.             for (int j = i + 1; j < matrix.GetLength(0); j++)
  155.             {
  156.                 for (int h = 0; h < matrix.GetLength(1); h++)
  157.                 {
  158.                     for (int d = 0; d < matrix.GetLength(2); d++)
  159.                     {
  160.                         sumTwo = sumTwo + matrix[j, h, d];
  161.                     }
  162.                 }
  163.             }
  164.  
  165.             if (sumOne == sumTwo)
  166.             {
  167.                 countEqualSums++;
  168.                 sumTwo = 0;
  169.                 sumOne = 0;
  170.             }
  171.             else
  172.             {
  173.                 sumTwo = 0;
  174.                 sumOne = 0;
  175.                 continue;
  176.             }
  177.         }
  178.         return countEqualSums;
  179.     }
  180.  
  181.     private static int[,,] ReadTheInput(int matrixWidth, int matrixHeigth, int matrixDepth, string[] tempLines)
  182.     {
  183.         int[,,] matrix = new int[matrixWidth, matrixHeigth, matrixDepth];
  184.         for (int i = 0; i < matrix.GetLength(1); i++)
  185.         {
  186.             string[] depthsStr = tempLines[i].Split('|');
  187.             for (int j = 0; j < matrix.GetLength(2); j++)
  188.             {
  189.                 string[] widthStr = depthsStr[j].Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  190.                 for (int k = 0; k < matrix.GetLength(0); k++)
  191.                 {
  192.                     matrix[k, i, j] = int.Parse(widthStr[k].Trim());
  193.                 }
  194.             }
  195.         }
  196.         return matrix;
  197.     }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement