stak441

3D Slices

Feb 3rd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04._3DSlices
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string dimensions = Console.ReadLine();
  14.             string[] sizes = dimensions.Split();
  15.             int width = int.Parse(sizes[0]);
  16.             int height = int.Parse(sizes[1]);
  17.             int depth = int.Parse(sizes[2]);
  18.            
  19.             int[, ,] cube = new int[width, height, depth];
  20.             long totalSum = 0;
  21.             for (int i = 0; i < height; i++)
  22.             {
  23.                 string row = Console.ReadLine();
  24.                 char[] seperator = {'|'};
  25.                 string[] numbers = row.Split(seperator);
  26.                 for (int j = 0; j < depth; j++)
  27.                 {
  28.                     string[] newLines = numbers[j].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  29.                        
  30.                     for (int k = 0; k < width; k++)
  31.                     {
  32.                         try                              //This was added because several tests return OutOfRangeException
  33.                         {
  34.                             int value = int.Parse(newLines[k]);
  35.                             cube[k, i, j] = value;
  36.                             totalSum += value;
  37.                         }
  38.                         catch (IndexOutOfRangeException)
  39.                         {
  40.                             Console.WriteLine("i {0}, j {1}, k {2}", i, j, k);
  41.                             throw;
  42.                         }
  43.                        
  44.                        
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             if (width == 1 && height == 1 && depth == 1)
  50.             {
  51.                 Console.WriteLine(0);
  52.             }
  53.             else
  54.             {
  55.                 int heightCombinations = CheckHeight(cube, width, height, depth, totalSum);
  56.                 int depthCombinations = CheckDepth(cube, width, height, depth, totalSum);
  57.                 int widthCombinations = CheckWidth(cube, width, height, depth, totalSum);
  58.  
  59.                 Console.WriteLine(heightCombinations + depthCombinations + widthCombinations);
  60.             }
  61.            
  62.  
  63.             //for (int i = 0; i < height; i++)
  64.             //{
  65.  
  66.             //    for (int j = 0; j < depth; j++)
  67.             //    {
  68.             //        for (int k = 0; k < width; k++)
  69.             //        {
  70.             //            Console.Write(cube[k, i, j]);
  71.             //        }
  72.             //        Console.Write('|');
  73.             //    }
  74.             //    Console.WriteLine();
  75.             //}
  76.         }
  77.  
  78.         static int CheckWidth(int[, ,] cube, int width, int height, int depth, long totalSum)
  79.         {
  80.             int combinations = 0;
  81.             int totalWidthSum = 0;
  82.  
  83.             for (int w = 0; w < width - 1; w++)
  84.             {
  85.                 for (int h = 0; h < height; h++)
  86.                 {
  87.                     for (int d = 0; d < depth; d++)
  88.                     {
  89.                         totalWidthSum += cube[w, h, d];
  90.                     }
  91.                 }
  92.                 if (totalWidthSum * 2 == totalSum)
  93.                 {
  94.                     combinations++;
  95.                 }
  96.             }
  97.             return combinations;
  98.         }
  99.  
  100.         static int CheckDepth(int[, ,] cube, int width, int height, int depth, long totalSum)
  101.         {
  102.             int combinations = 0;
  103.             int totalDepthSum = 0;
  104.  
  105.             for (int d = 0; d < depth - 1; d++)
  106.             {
  107.                 for (int h = 0; h < height; h++)
  108.                 {
  109.                     for (int w = 0; w < width; w++)
  110.                     {
  111.                         totalDepthSum += cube[w, h, d];
  112.                     }
  113.                 }
  114.                 if (totalDepthSum * 2 == totalSum)
  115.                 {
  116.                     combinations++;
  117.                 }
  118.             }
  119.             return combinations;
  120.         }
  121.  
  122.         static int CheckHeight(int[, ,] cube, int width, int height, int depth, long totalSum)
  123.         {
  124.             int combinations = 0;            
  125.             int totalHeightSum = 0;
  126.  
  127.             for (int i = 0; i < height - 1; i++)
  128.             {                
  129.                 for (int j = 0; j < depth; j++)
  130.                 {
  131.                     for (int k = 0; k < width; k++)
  132.                     {
  133.                         totalHeightSum += cube[k, i, j];
  134.                     }                    
  135.                 }
  136.                 if (totalHeightSum*2 == totalSum)
  137.                 {
  138.                     combinations++;
  139.                 }
  140.             }
  141.             return combinations;
  142.         }
  143.  
  144.        
  145.     }
  146. }
Add Comment
Please, Sign In to add comment