Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- //You are given a rectangular cuboid of size W (width), H (height) and D (depth) consisting of W * H * D cubes, each containing an integer number.
- //A 3D max walk in the cuboid starts from the cube located at the cuboid's center (W, H and D are odd numbers). At each step the walk continues
- //from the current cube in one of the 6 possible directions (left, right, up, down, deeper, shallower) to the cube which holds the maximal
- //value among all possible cubes different than the current. The walk stops at some of the following conditions:
- //• Several cubes hold the same maximal value.
- //• There is only cube holding the maximal value but it is already visited (falls into a loop).
- //Your task is to write a program that finds the sum of the numbers in the cubes that are visited during the 3D max walk.
- class Program
- {
- static int result;
- static void Main()
- {
- int width; // x
- int height; // y
- int depth; // z
- string[] input = Console.ReadLine().Split();
- width = int.Parse(input[0]);
- height = int.Parse(input[1]);
- depth = int.Parse(input[2]);
- int[, ,] cube = new int[width, height, depth];
- bool[, ,] visited = new bool[width, height, depth];
- string[] rows = new string[height]; // y
- string[] columns = new string[depth]; // z
- string[] cells = new string[width]; // x
- for (int y = 0; y < height; y++)
- {
- rows[y] = Console.ReadLine();
- //rows[0] = "3 4 1 9 1 | 0 1 2 3 8 | 1 2 5 6 7";
- //rows[1] = "2 7 3 1 9 | 2 5 5 2 1 | 8 6 3 5 8";
- //rows[2] = "1 8 2 1 5 | 9 1 3 8 6 | 4 5 6 3 2";
- for (int z = 0; z < depth; z++)
- {
- columns = rows[y].Split(new string[] { " | " }, StringSplitOptions.None);
- cells = columns[z].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- for (int x = 0; x < width; x++)
- {
- cube[x, y, z] = int.Parse(cells[x]);
- }
- }
- }
- //PrintCube(cube);
- CheckPositions(cube, visited, width, height, depth);
- }
- private static void CheckPositions(int[, ,] cube, bool[, ,] visited, int width, int height, int depth)
- {
- Position current = new Position(width / 2, height / 2, depth / 2);
- result += cube[current.x, current.y, current.z];
- visited[current.x, current.y, current.z] = true;
- TryAllPositions(cube, visited, width, height, depth, current);
- }
- private static void TryAllPositions(int[, ,] cube, bool[, ,] visited, int width, int height, int depth, Position current)
- {
- List<Position> posiblePositions = new List<Position>();
- if (current.x + 1 < width) // x +
- {
- for (int i = current.x + 1; i < width; i++)
- {
- posiblePositions.Add(new Position(i, current.y, current.z));
- }
- }
- if (current.x - 1 >= 0) // x -
- {
- for (int i = current.x - 1; i >= 0; i--)
- {
- posiblePositions.Add(new Position(i, current.y, current.z));
- }
- }
- if (current.y + 1 < height) // y +
- {
- for (int i = current.y + 1; i < height; i++)
- {
- posiblePositions.Add(new Position(current.x, i, current.z));
- }
- }
- if (current.y - 1 >= 0) // y -
- {
- for (int i = current.y - 1; i >= 0; i--)
- {
- posiblePositions.Add(new Position(current.x, i, current.z));
- }
- }
- if (current.z + 1 < depth) // z +
- {
- for (int i = current.z + 1; i < depth; i++)
- {
- posiblePositions.Add(new Position(current.x, current.y, i));
- }
- }
- if (current.z - 1 >= 0) // z -
- {
- for (int i = current.z - 1; i >= 0; i--)
- {
- posiblePositions.Add(new Position(current.x, current.y, i));
- }
- }
- int maxValue = int.MinValue;
- bool isDuplicated = false;
- Position bestPosition = current;
- for (int i = 0; i < posiblePositions.Count; i++)
- {
- if (maxValue == cube[posiblePositions[i].x, posiblePositions[i].y, posiblePositions[i].z])
- {
- isDuplicated = true;
- }
- if (cube[posiblePositions[i].x, posiblePositions[i].y, posiblePositions[i].z] > maxValue)
- {
- maxValue = cube[posiblePositions[i].x, posiblePositions[i].y, posiblePositions[i].z];
- bestPosition = posiblePositions[i];
- isDuplicated = false;
- }
- }
- if (isDuplicated == false && (visited[bestPosition.x, bestPosition.y, bestPosition.z] == false))
- {
- current = bestPosition;
- result += cube[current.x, current.y, current.z];
- visited[current.x, current.y, current.z] = true;
- TryAllPositions(cube, visited, width, height, depth, current); //recursion
- }
- else
- {
- Console.WriteLine(result);
- Environment.Exit(0);
- }
- } //here set breakpoint !!!!!!!
- private static void PrintCube(int[, ,] cube)
- {
- for (int y = 0; y < cube.GetLength(1); y++)
- {
- for (int z = 0; z < cube.GetLength(2); z++)
- {
- for (int x = 0; x < cube.GetLength(0); x++)
- {
- Console.Write(cube[x, y, z]);
- }
- Console.Write(" ");
- }
- Console.WriteLine();
- }
- }
- }
- class Position
- {
- public int x;
- public int y;
- public int z;
- public Position(int x, int y, int z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement