Advertisement
sashomaga

Slice cube

Jan 30th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //You are given a rectangular cuboid of size W (width), H (height) and D (depth) consisting of W * H * D cubes,
  6. //each containing an integer number. A cuboid can be split into two sub-cuboids by slicing it over some of the planes
  7. //{x, y}, {x, z} or {y, z}.
  8. //The cuboid is given as layers of matrices holding integer numbers.
  9. //Your task is to write a program that finds in how many ways we can split the cuboid into two non-empty sub-cuboids
  10. //such that the sums of the numbers in the obtained sub-cuboids are equal.
  11. class Program
  12. {
  13.     static void Main()
  14.     {
  15.         int width, height, depth;
  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.         int[, ,] cube = new int[height, width, depth];
  21.  
  22.         Fill(width, height, depth, cube);
  23.  
  24.         //find sums of all dimintions
  25.         int[] sumHeight = new int[height];
  26.         int[] sumWidth = new int[width];
  27.         int[] sumDepth = new int[depth];
  28.  
  29.         for (int x = 0; x < height; x++)
  30.         {
  31.             for (int z = 0; z < depth; z++)
  32.             {
  33.                 for (int y = 0; y < width; y++)
  34.                 {
  35.                     sumHeight[x] += cube[x, y, z];
  36.                     sumWidth[y] += cube[x, y, z];
  37.                     sumDepth[z] += cube[x, y, z];
  38.                 }
  39.             }
  40.         }
  41.  
  42.         CalcMatches(sumHeight, sumWidth, sumDepth);
  43.  
  44.     }
  45.  
  46.     private static void CalcMatches(int[] sumHeight, int[] sumWidth, int[] sumDepth)
  47.     {
  48.         int sum1 = 0, sum2 = 0;
  49.         int count = 0;
  50.         //width
  51.         for (int k = 0; k < sumWidth.Length - 1; k++)
  52.         {
  53.             sum1 = 0;
  54.             sum2 = 0;
  55.             for (int i = 0; i < sumWidth.Length; i++)
  56.             {
  57.                 if (i <= k)
  58.                 {
  59.                     sum1 += sumWidth[i];
  60.                 }
  61.                 else
  62.                 {
  63.                     sum2 += sumWidth[i];
  64.                 }
  65.             }
  66.             if (sum1 == sum2)
  67.             {
  68.                 count++;
  69.             }
  70.         }
  71.         //height
  72.         for (int k = 0; k < sumHeight.Length - 1; k++)
  73.         {
  74.             sum1 = 0;
  75.             sum2 = 0;
  76.             for (int i = 0; i < sumHeight.Length; i++)
  77.             {
  78.                 if (i <= k)
  79.                 {
  80.                     sum1 += sumHeight[i];
  81.                 }
  82.                 else
  83.                 {
  84.                     sum2 += sumHeight[i];
  85.                 }
  86.             }
  87.             if (sum1 == sum2)
  88.             {
  89.                 count++;
  90.             }
  91.         }
  92.         //depth
  93.         for (int k = 0; k < sumDepth.Length - 1; k++)
  94.         {
  95.             sum1 = 0;
  96.             sum2 = 0;
  97.             for (int i = 0; i < sumDepth.Length; i++)
  98.             {
  99.                 if (i <= k)
  100.                 {
  101.                     sum1 += sumDepth[i];
  102.                 }
  103.                 else
  104.                 {
  105.                     sum2 += sumDepth[i];
  106.                 }
  107.             }
  108.             if (sum1 == sum2)
  109.             {
  110.                 count++;
  111.             }
  112.         }
  113.         Console.WriteLine(count);
  114.     }
  115.  
  116.     private static void Fill(int width, int height, int depth, int[, ,] cube)
  117.     {
  118.         string[] data = new string[height];
  119.         string[] row;
  120.         string[] cells;
  121.         for (int x = 0; x < height; x++)
  122.         {
  123.             data[x] = Console.ReadLine();
  124.             for (int z = 0; z < depth; z++)
  125.             {
  126.                 row = data[x].Split('|');
  127.                 char[] factor = { ' ' };
  128.                 cells = row[z].Split(factor, StringSplitOptions.RemoveEmptyEntries);
  129.                 for (int y = 0; y < width; y++)
  130.                 {
  131.                     cube[x, y, z] = int.Parse(cells[y]);
  132.                 }
  133.             }
  134.         }
  135.     }
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement