Advertisement
Guest User

Sample Exam 2011/2012 3D Max Walk

a guest
Jan 31st, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _04._3dwalkmax
  7. {
  8.     class _3dwalkmax
  9.     {
  10.         static bool[, ,] usedCells;
  11.         static int[, ,] cuboid;
  12.         static void Main(string[] args)
  13.         {
  14.             string dimensions = Console.ReadLine();
  15.             string[] splittedDimensions = dimensions.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  16.             int width = int.Parse(splittedDimensions[0]);
  17.             int height = int.Parse(splittedDimensions[1]);
  18.             int depth = int.Parse(splittedDimensions[2]);
  19.  
  20.             //int width = 3;
  21.             //int height = 3;
  22.             //int depth = 1;
  23.  
  24.             cuboid = new int[depth, height, width];
  25.             usedCells = new bool[depth, height, width];
  26.  
  27.             //string[] line = {
  28.             //                    "3 4 1 9 1 | 0 1 2 3 8 | 1 2 5 6 7",
  29.             //                    "2 7 3 1 9 | 2 5 5 2 1 | 8 6 3 5 8",
  30.             //                    "1 8 2 1 5 | 9 1 3 8 6 | 4 5 6 3 2"
  31.             //                };
  32.  
  33.             //string[] line = {
  34.             //                    "1 2 3",
  35.             //                    "5 6 4",
  36.             //                    "8 4 5"
  37.  
  38.             //                };
  39.             for (int rows = 0; rows < height; rows++)
  40.             {
  41.                 string line = Console.ReadLine();
  42.                 string[] splittedRows = /*line[rows]*/line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  43.                 for (int slices = 0; slices < splittedRows.Length; slices++)
  44.                 {
  45.                     string[] splittedCols = splittedRows[slices].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  46.                     for (int cols = 0; cols < splittedCols.Length; cols++)
  47.                     {
  48.                         cuboid[slices, rows, cols] = int.Parse(splittedCols[cols]);
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             int[] position = new int[4]; //values for current positions 0 -height, 1 -width, 2 -slice, 3 -maxNumber
  54.             position[0] = (depth / 2);
  55.             position[1] = (height / 2);
  56.             position[2] = (width / 2);
  57.             position[3] = cuboid[position[0], position[1], position[2]];
  58.  
  59.             int[] positionRow = new int[4];
  60.             positionRow[3] = -1111;
  61.             int[] positionCol = new int[4];
  62.             positionCol[3] = -1111;
  63.             int[] positionSlice = new int[4];
  64.             positionSlice[3] = -1111;
  65.  
  66.  
  67.             int totalSum = 0;
  68.  
  69.             while (CheckedCells(position) && CheckEquality(positionRow, positionCol, positionSlice, position[3]))
  70.             {
  71.                 totalSum += position[3];
  72.                 usedCells[position[0], position[1], position[2]] = true;
  73.  
  74.                 //PrintPosition(position);
  75.  
  76.                 positionRow = GetMaxRow(position);
  77.                 positionCol = GetMaxCol(position);
  78.                 positionSlice = GetMaxSlice(position);
  79.  
  80.                 position = GetMaxPostion(positionRow, positionCol, positionSlice);
  81.  
  82.  
  83.             }
  84.             Console.WriteLine(totalSum);
  85.         }
  86.  
  87.         private static bool CheckEquality(int[] positionRow, int[] positionCol, int[] positionSlice, int maxNumber)
  88.         {
  89.             int count = 0;
  90.             if (positionRow[3] == maxNumber)
  91.             {
  92.                 count++;
  93.             }
  94.             if (positionCol[3] == maxNumber)
  95.             {
  96.                 count++;
  97.             }
  98.             if (positionSlice[3] == maxNumber)
  99.             {
  100.                 count++;
  101.             }
  102.             if (count>1)
  103.             {
  104.                 return false;
  105.             }
  106.             else
  107.             {
  108.                 return true;
  109.             }
  110.         }
  111.  
  112.         private static void PrintPosition(int[] position)
  113.         {
  114.             Console.WriteLine("Slice: {0} Row: {1} Col: {2} Max: {3}", position[0], position[1], position[2], position[3]);
  115.         }
  116.  
  117.         private static int[] GetMaxPostion(int[] positionRow, int[] positionCol, int[] positionSlice)
  118.         {
  119.             if (positionRow[3] >= positionCol[3] && positionRow[3] >= positionSlice[3])
  120.             {
  121.                 return positionRow;
  122.             }
  123.             else if (positionCol[3] >= positionRow[3] && positionCol[3] >= positionSlice[3])
  124.             {
  125.                 return positionCol;
  126.             }
  127.             else
  128.             {
  129.                 return positionSlice;
  130.             }
  131.         }
  132.  
  133.         private static int[] GetMaxSlice(int[] position)
  134.         {
  135.             int[] positionSlice = new int[4];
  136.             positionSlice[1] = position[1];
  137.             positionSlice[2] = position[2];
  138.             int maxNumber = -1111;
  139.             int length = cuboid.GetLength(0);
  140.             for (int i = 0; i < length; i++)
  141.             {
  142.                 if (i != position[0])
  143.                 {
  144.                     if (cuboid[i, position[1], position[2]] > maxNumber)
  145.                     {
  146.                         maxNumber = cuboid[i, position[1], position[2]];
  147.                         positionSlice[0] = i;
  148.                     }
  149.                 }
  150.             }
  151.             positionSlice[3] = maxNumber;
  152.  
  153.             return positionSlice;
  154.         }
  155.  
  156.         private static int[] GetMaxCol(int[] position)
  157.         {
  158.             int[] positionCol = new int[4];
  159.             positionCol[0] = position[0];
  160.             positionCol[1] = position[1];
  161.             int maxNumber = -1111;
  162.             int length = cuboid.GetLength(2);
  163.             for (int i = 0; i < length; i++)
  164.             {
  165.                 if (i != position[2])
  166.                 {
  167.                     if (cuboid[position[0], position[1], i] > maxNumber)
  168.                     {
  169.                         maxNumber = cuboid[position[0], position[1], i];
  170.                         positionCol[2] = i;
  171.                     }
  172.                 }
  173.             }
  174.             positionCol[3] = maxNumber;
  175.  
  176.             return positionCol;
  177.         }
  178.  
  179.         private static int[] GetMaxRow(int[] position)
  180.         {
  181.             int[] positionRow = new int[4];
  182.             positionRow[0] = position[0];
  183.             positionRow[2] = position[2];
  184.             int maxNumber = -1111;
  185.             int length = cuboid.GetLength(1);
  186.             for (int i = 0; i < length; i++)
  187.             {
  188.                 if (i != position[1])
  189.                 {
  190.                     if (cuboid[position[0], i, position[2]] > maxNumber)
  191.                     {
  192.                         maxNumber = cuboid[position[0], i, position[2]];
  193.                         positionRow[1] = i;
  194.                     }
  195.                 }
  196.             }
  197.             positionRow[3] = maxNumber;
  198.  
  199.             return positionRow;
  200.         }
  201.  
  202.         private static bool CheckedCells(int[] position)
  203.         {
  204.             if (usedCells[position[0], position[1], position[2]] == true)
  205.             {
  206.                 return false;
  207.             }
  208.  
  209.             return true;
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement