Guest User

Untitled

a guest
Feb 1st, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class MaxWalk
  7. {
  8.     static readonly string[] firstLine = Console.ReadLine().Split(' ');
  9.     static int W = int.Parse(firstLine[0]);
  10.     static int H = int.Parse(firstLine[1]);
  11.     static int D = int.Parse(firstLine[2]);
  12.     static readonly bool[, ,] visitedCuboid = new bool[W, H, D];
  13.     static readonly int[] totalMaxCoordinations = new int[3];
  14.  
  15.     static string checkMax(int[,,] cuboid, int width, int height, int depth)
  16.     {
  17.         int defaultWidth = width;
  18.         int defaultHeight = height;
  19.         int defaultDepth = depth;
  20.  
  21.         int totalMax = checkTotalMax(cuboid, defaultWidth, defaultHeight, defaultDepth);
  22.  
  23.         int maxCount = int.MinValue;
  24.         int left = int.MinValue;
  25.         width = defaultWidth;
  26.         width -= 1;
  27.         int positionLeft = 0;
  28.         while (width >= 0)
  29.         {
  30.             if (cuboid[width, defaultHeight, defaultDepth] == totalMax && (width != totalMaxCoordinations[0] || defaultHeight != totalMaxCoordinations[1] || defaultDepth != totalMaxCoordinations[2]))
  31.             {
  32.                 return "break";
  33.             }
  34.             if (cuboid[width, defaultHeight, defaultDepth] > maxCount)
  35.             {
  36.                 maxCount = cuboid[width, defaultHeight, defaultDepth];
  37.                 positionLeft = width;
  38.             }
  39.             width -= 1;
  40.         }
  41.         left = maxCount;
  42.  
  43.         maxCount = int.MinValue;
  44.         int right = int.MinValue;
  45.         width = defaultWidth;
  46.         width += 1;
  47.         int positionRight = 0;
  48.         while (width < W)
  49.         {
  50.             if (cuboid[width, defaultHeight, defaultDepth] == totalMax && (width != totalMaxCoordinations[0] || defaultHeight != totalMaxCoordinations[1] || defaultDepth != totalMaxCoordinations[2]))
  51.             {
  52.                 return "break";
  53.             }
  54.  
  55.             if (cuboid[width, defaultHeight, defaultDepth] > maxCount)
  56.             {
  57.                 maxCount = cuboid[width, defaultHeight, defaultDepth];
  58.                 positionRight = width;
  59.             }
  60.             width += 1;
  61.         }
  62.         right = maxCount;
  63.  
  64.         maxCount = int.MinValue;
  65.         int down = int.MinValue;
  66.         height = defaultHeight;
  67.         height -= 1;
  68.         int positionDown = 0;
  69.         while (height >= 0)
  70.         {
  71.             if (cuboid[defaultWidth, height, defaultDepth] == totalMax && (defaultWidth != totalMaxCoordinations[0] || height != totalMaxCoordinations[1] || defaultDepth != totalMaxCoordinations[2]))
  72.             {
  73.                 return "break";
  74.             }
  75.  
  76.             if (cuboid[defaultWidth, height, defaultDepth] > maxCount)
  77.             {
  78.                 maxCount = cuboid[defaultWidth, height, defaultDepth];
  79.                 positionDown = height;
  80.             }
  81.             height -= 1;
  82.         }
  83.         down = maxCount;
  84.  
  85.         maxCount = int.MinValue;
  86.         int up = int.MinValue;
  87.         height = defaultHeight;
  88.         height += 1;
  89.         int positionUp = 0;
  90.         while (height < H)
  91.         {
  92.             if (cuboid[defaultWidth, height, defaultDepth] == totalMax && (defaultWidth != totalMaxCoordinations[0] || height != totalMaxCoordinations[1] || defaultDepth != totalMaxCoordinations[2]))
  93.             {
  94.                 return "break";
  95.             }
  96.  
  97.             if (cuboid[defaultWidth, height, defaultDepth] > maxCount)
  98.             {
  99.                 maxCount = cuboid[defaultWidth, height, defaultDepth];
  100.                 positionUp = height;
  101.             }
  102.             height += 1;
  103.         }
  104.         up = maxCount;
  105.  
  106.         maxCount = int.MinValue;
  107.         int shallow = int.MinValue;
  108.         depth = defaultDepth;
  109.         depth -= 1;
  110.         int positionShallow = 0;
  111.         while (depth >= 0)
  112.         {
  113.             if (cuboid[defaultWidth, defaultHeight, depth] == totalMax && (defaultWidth != totalMaxCoordinations[0] || defaultHeight != totalMaxCoordinations[1] || depth != totalMaxCoordinations[2]))
  114.             {
  115.                 return "break";
  116.             }
  117.  
  118.             if (cuboid[defaultWidth, defaultHeight, depth] > maxCount)
  119.             {
  120.                 maxCount = cuboid[defaultWidth, defaultHeight, depth];
  121.                 positionShallow = depth;
  122.             }
  123.             depth -= 1;
  124.         }
  125.         shallow = maxCount;
  126.  
  127.         maxCount = int.MinValue;
  128.         int deep = int.MinValue;
  129.         depth = defaultDepth;
  130.         depth += 1;
  131.         int positionDeep = 0;
  132.         while (depth < D)
  133.         {
  134.             if (cuboid[defaultWidth, defaultHeight, depth] == totalMax && (defaultWidth != totalMaxCoordinations[0] || defaultHeight != totalMaxCoordinations[1] || depth != totalMaxCoordinations[2]))
  135.             {
  136.                 return "break";
  137.             }
  138.  
  139.             if (cuboid[defaultWidth, defaultHeight, depth] > maxCount)
  140.             {
  141.                 maxCount = cuboid[defaultWidth, defaultHeight, depth];
  142.                 positionDeep = depth;
  143.             }
  144.             depth += 1;
  145.         }
  146.         deep = maxCount;
  147.  
  148.         if (left > right && left > down && left > up && left > shallow && left > deep)
  149.         {
  150.             if (visitedCuboid[positionLeft, defaultHeight, defaultDepth])
  151.             {
  152.                 return "break";
  153.             }
  154.             return "left";
  155.         }
  156.         if (right > left && right > down && right > up && right > shallow && right > deep)
  157.         {
  158.             if (visitedCuboid[positionRight, defaultHeight, defaultDepth])
  159.             {
  160.                 return "break";
  161.             }
  162.             return "right";
  163.         }
  164.         if (up > left && up > right && up > down && up > shallow && up > deep)
  165.         {
  166.             if (visitedCuboid[defaultWidth, positionUp, defaultDepth])
  167.             {
  168.                 return "break";
  169.             }
  170.             return "up";
  171.         }
  172.         if (down > left && down > right && down > up && down > shallow && down > deep)
  173.         {
  174.             if (visitedCuboid[defaultWidth, positionDown, defaultDepth])
  175.             {
  176.                 return "break";
  177.             }
  178.             return "down";
  179.         }
  180.         if (shallow > left && shallow > right && shallow > up && shallow > down && shallow > deep)
  181.         {
  182.             if (visitedCuboid[defaultWidth, defaultHeight, positionShallow])
  183.             {
  184.                 return "break";
  185.             }
  186.             return "shallow";
  187.         }
  188.         if (deep > left && deep > right && deep > up && deep > down && deep > shallow)
  189.         {
  190.             if (visitedCuboid[defaultWidth, defaultHeight, positionDeep])
  191.             {
  192.                 return "break";
  193.             }
  194.             return "deep";
  195.         }
  196.  
  197.         return "break";
  198.     }
  199.  
  200.     static int checkTotalMax(int[, ,] cuboid, int width, int height, int depth)
  201.     {
  202.         int totalMax = int.MinValue;
  203.         int defaultWidth = width;
  204.         int defaultHeight = height;
  205.         int defaultDepth = depth;
  206.  
  207.         int maxCount = int.MinValue;
  208.         width = defaultWidth;
  209.         width -= 1;
  210.         while (width >= 0)
  211.         {
  212.             if (cuboid[width, defaultHeight, defaultDepth] > totalMax)
  213.             {
  214.                 totalMax = cuboid[width, defaultHeight, defaultDepth];
  215.                 totalMaxCoordinations[0] = width;
  216.                 totalMaxCoordinations[1] = defaultHeight;
  217.                 totalMaxCoordinations[2] = defaultDepth;
  218.             }
  219.             width -= 1;
  220.         }
  221.  
  222.         maxCount = int.MinValue;
  223.         width = defaultWidth;
  224.         width += 1;
  225.         while (width < W)
  226.         {
  227.             if (cuboid[width, defaultHeight, defaultDepth] > totalMax)
  228.             {
  229.                 totalMax = cuboid[width, defaultHeight, defaultDepth];
  230.                 totalMaxCoordinations[0] = width;
  231.                 totalMaxCoordinations[1] = defaultHeight;
  232.                 totalMaxCoordinations[2] = defaultDepth;
  233.             }
  234.             width += 1;
  235.         }
  236.  
  237.         maxCount = int.MinValue;
  238.         height = defaultHeight;
  239.         height -= 1;
  240.         while (height >= 0)
  241.         {
  242.             if (cuboid[defaultWidth, height, defaultDepth] > totalMax)
  243.             {
  244.                 totalMax = cuboid[defaultWidth, height, defaultDepth];
  245.                 totalMaxCoordinations[0] = defaultWidth;
  246.                 totalMaxCoordinations[1] = height;
  247.                 totalMaxCoordinations[2] = defaultDepth;
  248.             }
  249.             height -= 1;
  250.         }
  251.  
  252.         maxCount = int.MinValue;
  253.         height = defaultHeight;
  254.         height += 1;
  255.         while (height < H)
  256.         {
  257.             if (cuboid[defaultWidth, height, defaultDepth] > totalMax)
  258.             {
  259.                 totalMax = cuboid[defaultWidth, height, defaultDepth];
  260.                 totalMaxCoordinations[0] = defaultWidth;
  261.                 totalMaxCoordinations[1] = height;
  262.                 totalMaxCoordinations[2] = defaultDepth;
  263.             }
  264.             height += 1;
  265.         }
  266.  
  267.         maxCount = int.MinValue;
  268.         depth = defaultDepth;
  269.         depth -= 1;
  270.         while (depth >= 0)
  271.         {
  272.             if (cuboid[defaultWidth, defaultHeight, depth] > totalMax)
  273.             {
  274.                 totalMax = cuboid[defaultWidth, defaultHeight, depth];
  275.                 totalMaxCoordinations[0] = defaultWidth;
  276.                 totalMaxCoordinations[1] = defaultHeight;
  277.                 totalMaxCoordinations[2] = depth;
  278.             }
  279.             depth -= 1;
  280.         }
  281.  
  282.         maxCount = int.MinValue;
  283.         depth = defaultDepth;
  284.         depth += 1;
  285.         while (depth < D)
  286.         {
  287.             if (cuboid[defaultWidth, defaultHeight, depth] > totalMax)
  288.             {
  289.                 totalMax = cuboid[defaultWidth, defaultHeight, depth];
  290.                 totalMaxCoordinations[0] = defaultWidth;
  291.                 totalMaxCoordinations[1] = defaultHeight;
  292.                 totalMaxCoordinations[2] = depth;
  293.             }
  294.             depth += 1;
  295.         }
  296.  
  297.         return totalMax;
  298.     }
  299.  
  300.     static void Main()
  301.     {
  302.         int[, ,] cuboid = new int[W, H, D];
  303.  
  304.         for (int i = 0; i < H; i++)
  305.         {
  306.             string height = Console.ReadLine();
  307.             string[] depth = height.Split('|');
  308.  
  309.             for (int j = 0; j < D; j++)
  310.             {
  311.                 string currentDepth = depth[j];
  312.                 currentDepth = currentDepth.Trim();
  313.  
  314.                 string[] currentWidth = currentDepth.Split(' ');
  315.  
  316.                 for (int k = 0; k < W; k++)
  317.                 {
  318.                      cuboid[k, i, j] = int.Parse(currentWidth[k]);
  319.                 }
  320.             }
  321.         }
  322.  
  323.         int currentWidthNumber = W / 2;
  324.         int currentHeightNumber = H / 2;
  325.         int currentDepthNumber = D / 2;
  326.         int currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  327.         long sum = currentNumber;
  328.  
  329.         while (true)
  330.         {
  331.             string direction = checkMax(cuboid, currentWidthNumber, currentHeightNumber, currentDepthNumber);
  332.             visitedCuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber] = true;
  333.  
  334.             if (direction == "left")
  335.             {
  336.                 int widthCheck = currentWidthNumber;
  337.                 widthCheck -= 1;
  338.                 int maxNumber = int.MinValue;
  339.  
  340.                 while (widthCheck >= 0)
  341.                 {
  342.                     if (cuboid[widthCheck, currentHeightNumber, currentDepthNumber] > maxNumber)
  343.                     {
  344.                         maxNumber = cuboid[widthCheck, currentHeightNumber, currentDepthNumber];
  345.                         currentWidthNumber = widthCheck;
  346.                     }
  347.                     widthCheck -= 1;
  348.                 }
  349.  
  350.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  351.                 sum += currentNumber;
  352.             }
  353.             else if (direction == "right")
  354.             {
  355.                 int widthCheck = currentWidthNumber;
  356.                 widthCheck += 1;
  357.                 int maxNumber = int.MinValue;
  358.  
  359.                 while (widthCheck < W)
  360.                 {
  361.                     if (cuboid[widthCheck, currentHeightNumber, currentDepthNumber] > maxNumber)
  362.                     {
  363.                         maxNumber = cuboid[widthCheck, currentHeightNumber, currentDepthNumber];
  364.                         currentWidthNumber = widthCheck;
  365.                     }
  366.                     widthCheck += 1;
  367.                 }
  368.  
  369.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  370.                 sum += currentNumber;
  371.             }
  372.             else if (direction == "down")
  373.             {
  374.                 int heightCheck = currentHeightNumber;
  375.                 heightCheck -= 1;
  376.                 int maxNumber = int.MinValue;
  377.  
  378.                 while (heightCheck >= 0)
  379.                 {
  380.                     if (cuboid[currentWidthNumber, heightCheck, currentDepthNumber] > maxNumber)
  381.                     {
  382.                         maxNumber = cuboid[currentWidthNumber, heightCheck, currentDepthNumber];
  383.                         currentHeightNumber = heightCheck;
  384.                     }
  385.                     heightCheck -= 1;
  386.                 }
  387.  
  388.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  389.                 sum += currentNumber;
  390.             }
  391.             else if (direction == "up")
  392.             {
  393.                 int heightCheck = currentHeightNumber;
  394.                 heightCheck += 1;
  395.                 int maxNumber = int.MinValue;
  396.  
  397.                 while (heightCheck < H)
  398.                 {
  399.                     if (cuboid[currentWidthNumber, heightCheck, currentDepthNumber] > maxNumber)
  400.                     {
  401.                         maxNumber = cuboid[currentWidthNumber, heightCheck, currentDepthNumber];
  402.                         currentHeightNumber = heightCheck;
  403.                     }
  404.                     heightCheck += 1;
  405.                 }
  406.  
  407.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  408.                 sum += currentNumber;
  409.             }
  410.             else if (direction == "shallow")
  411.             {
  412.                 int depthCheck = currentDepthNumber;
  413.                 depthCheck -= 1;
  414.                 int maxNumber = int.MinValue;
  415.  
  416.                 while (depthCheck >= 0)
  417.                 {
  418.                     if (cuboid[currentWidthNumber, currentHeightNumber, depthCheck] > maxNumber)
  419.                     {
  420.                         maxNumber = cuboid[currentWidthNumber, currentHeightNumber, depthCheck];
  421.                         currentDepthNumber = depthCheck;
  422.                     }
  423.                     depthCheck -= 1;
  424.                 }
  425.  
  426.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  427.                 sum += currentNumber;
  428.             }
  429.             else if (direction == "deep")
  430.             {
  431.                 int depthCheck = currentDepthNumber;
  432.                 depthCheck += 1;
  433.                 int maxNumber = int.MinValue;
  434.  
  435.                 while (depthCheck < D)
  436.                 {
  437.                     if (cuboid[currentWidthNumber, currentHeightNumber, depthCheck] > maxNumber)
  438.                     {
  439.                         maxNumber = cuboid[currentWidthNumber, currentHeightNumber, depthCheck];
  440.                         currentDepthNumber = depthCheck;
  441.                     }
  442.                     depthCheck += 1;
  443.                 }
  444.  
  445.                 currentNumber = cuboid[currentWidthNumber, currentHeightNumber, currentDepthNumber];
  446.                 sum += currentNumber;
  447.             }
  448.             else if (direction == "break")
  449.             {
  450.                 break;
  451.             }
  452.         }
  453.         Console.WriteLine(sum);
  454.     }
  455. }
Add Comment
Please, Sign In to add comment