psotirov

Liquid Ver1

Feb 5th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2.  
  3. class Liquid
  4. {
  5.     static int[, ,] cuboid;
  6.  
  7.     static void Main()
  8.     {
  9.         // temporary console input redirection
  10.         //Console.SetIn(new System.IO.StreamReader("test.008.in.txt"));
  11.  
  12.         // reads dimensions of cuboid
  13.         string[] dims = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  14.         int dimW = int.Parse(dims[0]);
  15.         int dimD = int.Parse(dims[1]);
  16.         int dimH = int.Parse(dims[2]);
  17.  
  18.  
  19.         // creates cuboid array / each cell can contain: cuboid[W,H,D] e [0, 100]
  20.         cuboid = new int[dimW, dimD, dimH];
  21.  
  22.         // reads cuboid values
  23.         for (int iD = 0; iD < dimD; iD++)
  24.         {
  25.             string[] line = Console.ReadLine().Split(new char[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries);
  26.             for (int iH = 0; iH < dimH; iH++)
  27.             {
  28.                 for (int iW = 0; iW < dimW; iW++)
  29.                 {
  30.                     cuboid[iW, iD, iH] = int.Parse(line[iH * dimW + iW]);
  31.                 }
  32.             }
  33.         }
  34.  
  35.         int liquidOut = 0; // initially there is no output liquid amount
  36.         for (int iW = 0; iW < dimW; iW++)
  37.         {
  38.             for (int iD = 0; iD < dimD; iD++)
  39.             {
  40.                 if (cuboid[iW, iD, 0] > 0) // walks through all top cells that have some capacity
  41.                 {
  42.                     liquidOut += PourLiquid(iW, iD, 0); // and takes the liquid amount that could be poured through it
  43.                 }
  44.             }
  45.         }
  46.  
  47.         //outputs the result
  48.         Console.WriteLine(liquidOut);
  49.     }
  50.  
  51.     static int PourLiquid(int cellW, int cellD, int cellH, int reqAmount = 101)
  52.     {
  53.         // if current cell has no capacity exits immediately
  54.         if (cuboid[cellW, cellD, cellH] == 0) return 0;
  55.  
  56.         // initially we don't have any pouring capacity
  57.         int outAmount = 0;
  58.  
  59.         // the total flow could not exceed current cell capacity
  60.         if (reqAmount > cuboid[cellW, cellD, cellH]) reqAmount = cuboid[cellW, cellD, cellH];
  61.  
  62.         // if the cell is at the bottom then returns its full capacity
  63.         if (cellH == cuboid.GetLength(2) - 1)
  64.         {
  65.             outAmount = cuboid[cellW, cellD, cellH]; // output capacity is unlimited
  66.             if (reqAmount < outAmount)
  67.             {
  68.                 outAmount = reqAmount; // ensures pouring only of requested amount if it is enough
  69.             }
  70.             cuboid[cellW, cellD, cellH] -= outAmount; // and uses its total capacity
  71.         }
  72.  
  73.         // otherwise we have to pour all requested capacity through all neighbour cells
  74.         else
  75.         {
  76.             cuboid[cellW, cellD, cellH] -= reqAmount; // using requested capacity (or less) from current cell
  77.             //cuboid[cellW, cellD, cellH] = 0; // using requested capacity (or less) from current cell
  78.  
  79.             // bottom cell first
  80.             outAmount = PourLiquid(cellW, cellD, cellH + 1, reqAmount);
  81.  
  82.             // checks for all other neighbours if they can supply the requested capacity
  83.             // to left
  84.             if (outAmount < reqAmount && cellW > 0) outAmount += PourLiquid(cellW - 1, cellD, cellH, reqAmount - outAmount);
  85.             // to shallower
  86.             if (outAmount < reqAmount && cellD > 0) outAmount += PourLiquid(cellW, cellD - 1, cellH, reqAmount - outAmount);
  87.             // to right
  88.             if (outAmount < reqAmount && cellW < cuboid.GetLength(0) - 1) outAmount += PourLiquid(cellW + 1, cellD, cellH, reqAmount - outAmount);
  89.             // to deeper
  90.             if (outAmount < reqAmount && cellD < cuboid.GetLength(1) - 1) outAmount += PourLiquid(cellW, cellD + 1, cellH, reqAmount - outAmount);
  91.         }
  92.  
  93.         return outAmount; // returns possible capacity
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment