Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _04._3dwalkmax
- {
- class _3dwalkmax
- {
- static bool[, ,] usedCells;
- static int[, ,] cuboid;
- static void Main(string[] args)
- {
- string dimensions = Console.ReadLine();
- string[] splittedDimensions = dimensions.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- int width = int.Parse(splittedDimensions[0]);
- int height = int.Parse(splittedDimensions[1]);
- int depth = int.Parse(splittedDimensions[2]);
- //int width = 3;
- //int height = 3;
- //int depth = 1;
- cuboid = new int[depth, height, width];
- usedCells = new bool[depth, height, width];
- //string[] line = {
- // "3 4 1 9 1 | 0 1 2 3 8 | 1 2 5 6 7",
- // "2 7 3 1 9 | 2 5 5 2 1 | 8 6 3 5 8",
- // "1 8 2 1 5 | 9 1 3 8 6 | 4 5 6 3 2"
- // };
- //string[] line = {
- // "1 2 3",
- // "5 6 4",
- // "8 4 5"
- // };
- for (int rows = 0; rows < height; rows++)
- {
- string line = Console.ReadLine();
- string[] splittedRows = /*line[rows]*/line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- for (int slices = 0; slices < splittedRows.Length; slices++)
- {
- string[] splittedCols = splittedRows[slices].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- for (int cols = 0; cols < splittedCols.Length; cols++)
- {
- cuboid[slices, rows, cols] = int.Parse(splittedCols[cols]);
- }
- }
- }
- int[] position = new int[4]; //values for current positions 0 -height, 1 -width, 2 -slice, 3 -maxNumber
- position[0] = (depth / 2);
- position[1] = (height / 2);
- position[2] = (width / 2);
- position[3] = cuboid[position[0], position[1], position[2]];
- int[] positionRow = new int[4];
- positionRow[3] = -1111;
- int[] positionCol = new int[4];
- positionCol[3] = -1111;
- int[] positionSlice = new int[4];
- positionSlice[3] = -1111;
- int totalSum = 0;
- while (CheckedCells(position) && CheckEquality(positionRow, positionCol, positionSlice, position[3]))
- {
- totalSum += position[3];
- usedCells[position[0], position[1], position[2]] = true;
- //PrintPosition(position);
- positionRow = GetMaxRow(position);
- positionCol = GetMaxCol(position);
- positionSlice = GetMaxSlice(position);
- position = GetMaxPostion(positionRow, positionCol, positionSlice);
- }
- Console.WriteLine(totalSum);
- }
- private static bool CheckEquality(int[] positionRow, int[] positionCol, int[] positionSlice, int maxNumber)
- {
- int count = 0;
- if (positionRow[3] == maxNumber)
- {
- count++;
- }
- if (positionCol[3] == maxNumber)
- {
- count++;
- }
- if (positionSlice[3] == maxNumber)
- {
- count++;
- }
- if (count>1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private static void PrintPosition(int[] position)
- {
- Console.WriteLine("Slice: {0} Row: {1} Col: {2} Max: {3}", position[0], position[1], position[2], position[3]);
- }
- private static int[] GetMaxPostion(int[] positionRow, int[] positionCol, int[] positionSlice)
- {
- if (positionRow[3] >= positionCol[3] && positionRow[3] >= positionSlice[3])
- {
- return positionRow;
- }
- else if (positionCol[3] >= positionRow[3] && positionCol[3] >= positionSlice[3])
- {
- return positionCol;
- }
- else
- {
- return positionSlice;
- }
- }
- private static int[] GetMaxSlice(int[] position)
- {
- int[] positionSlice = new int[4];
- positionSlice[1] = position[1];
- positionSlice[2] = position[2];
- int maxNumber = -1111;
- int length = cuboid.GetLength(0);
- for (int i = 0; i < length; i++)
- {
- if (i != position[0])
- {
- if (cuboid[i, position[1], position[2]] > maxNumber)
- {
- maxNumber = cuboid[i, position[1], position[2]];
- positionSlice[0] = i;
- }
- }
- }
- positionSlice[3] = maxNumber;
- return positionSlice;
- }
- private static int[] GetMaxCol(int[] position)
- {
- int[] positionCol = new int[4];
- positionCol[0] = position[0];
- positionCol[1] = position[1];
- int maxNumber = -1111;
- int length = cuboid.GetLength(2);
- for (int i = 0; i < length; i++)
- {
- if (i != position[2])
- {
- if (cuboid[position[0], position[1], i] > maxNumber)
- {
- maxNumber = cuboid[position[0], position[1], i];
- positionCol[2] = i;
- }
- }
- }
- positionCol[3] = maxNumber;
- return positionCol;
- }
- private static int[] GetMaxRow(int[] position)
- {
- int[] positionRow = new int[4];
- positionRow[0] = position[0];
- positionRow[2] = position[2];
- int maxNumber = -1111;
- int length = cuboid.GetLength(1);
- for (int i = 0; i < length; i++)
- {
- if (i != position[1])
- {
- if (cuboid[position[0], i, position[2]] > maxNumber)
- {
- maxNumber = cuboid[position[0], i, position[2]];
- positionRow[1] = i;
- }
- }
- }
- positionRow[3] = maxNumber;
- return positionRow;
- }
- private static bool CheckedCells(int[] position)
- {
- if (usedCells[position[0], position[1], position[2]] == true)
- {
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement