Advertisement
DennyGD

Slides

Mar 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Slides
  5. {
  6.     class Slides
  7.     {
  8.         static void Main()
  9.         {
  10.             string[] readDimensions = Console.ReadLine().Split(' ');
  11.             int width = Int32.Parse(readDimensions[0]);
  12.             int height = Int32.Parse(readDimensions[1]);
  13.             int depth = Int32.Parse(readDimensions[2]);
  14.  
  15.             // fill cuboid
  16.             string[, ,] cuboid = new string[width, height, depth];
  17.  
  18.             for (int h = 0; h < height; h++)
  19.             {
  20.                 string[] floor = Console.ReadLine().Split('|');
  21.                 for (int d = 0; d < depth; d++)
  22.                 {
  23.                     string[] separateByBrackets = floor[d].Trim().Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
  24.                     for (int w = 0; w < width; w++)
  25.                     {
  26.                         string cell = separateByBrackets[w];
  27.                         cuboid[w, h, d] = cell;
  28.                     }
  29.                 }
  30.             }
  31.  
  32.             // get ball position
  33.             readDimensions = Console.ReadLine().Split(' ');
  34.             int ballW = Int32.Parse(readDimensions[0]);
  35.             int ballH = 0;
  36.             int ballD = Int32.Parse(readDimensions[1]);
  37.             int nextBallW = ballW, nextBallH = ballH, nextBallD = ballD;
  38.             bool escaped = true;
  39.  
  40.             // start the game (drop the ball)
  41.             while (true)
  42.             {
  43.                 string instructions = cuboid[nextBallW, nextBallH, nextBallD];
  44.                 ballW = nextBallW;
  45.                 ballH = nextBallH;
  46.                 ballD = nextBallD;
  47.                
  48.                 if (instructions == "B")
  49.                 {
  50.                     escaped = false;
  51.                     break;
  52.                 }
  53.                
  54.                 // translate instructions into coordinates
  55.                 int[] nextCoordinates = CalculateCoordinates(instructions, ballW, ballH, ballD);
  56.                 nextBallW = nextCoordinates[0];
  57.                 nextBallH = nextCoordinates[1];
  58.                 nextBallD = nextCoordinates[2];
  59.                 // check coordinates
  60.                 if (instructions[0] != 'T')
  61.                 {
  62.                     if (ballH == height - 1)
  63.                     {
  64.                         escaped = true;
  65.                         break;
  66.                     }
  67.                     else
  68.                     {
  69.                         if ((nextBallW < 0) || (nextBallW > width - 1) || (nextBallD < 0) || (nextBallD > depth - 1))
  70.                         {
  71.                             escaped = false;
  72.                             break;
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             // print output
  79.             Console.WriteLine("{0}", escaped ? "Yes" : "No");
  80.             Console.WriteLine("{0} {1} {2}", ballW, ballH, ballD);
  81.         }
  82.  
  83.         static int[] CalculateCoordinates(string instructions, int ballW, int ballH, int ballD)
  84.         {
  85.             int[] coordinates = new int[3];
  86.             // handle the slide cases
  87.             if (instructions[0] == 'S')
  88.             {
  89.                 instructions = instructions.Replace(" ", "");
  90.             }
  91.             // handle SL, SR, SF, SB
  92.             if ((instructions[0] == 'S') && (instructions.Length == 2))
  93.             {
  94.                 // SL, SR, SF, SB
  95.                 coordinates[1] = ballH + 1;
  96.                 switch (instructions[1])
  97.                 {
  98.                     case 'L':
  99.                         coordinates[0] = ballW - 1;
  100.                         coordinates[2] = ballD;
  101.                         break;
  102.                     case 'R':
  103.                         coordinates[0] = ballW + 1;
  104.                         coordinates[2] = ballD;
  105.                         break;
  106.                     case 'F':
  107.                         coordinates[0] = ballW;
  108.                         coordinates[2] = ballD - 1;
  109.                         break;
  110.                     case 'B':
  111.                         coordinates[0] = ballW;
  112.                         coordinates[2] = ballD + 1;
  113.                         break;
  114.                 }
  115.             }
  116.             // handle SFL, SFR, SBL, SBR
  117.             else if ((instructions[0] == 'S') && (instructions.Length == 3))
  118.             {
  119.                 coordinates[1] = ballH + 1;
  120.                 switch (instructions[1])
  121.                 {
  122.                     case 'F':
  123.                         coordinates[2] = ballD - 1;
  124.                         if (instructions[2] == 'L')
  125.                         {
  126.                             coordinates[0] = ballW - 1;
  127.                         }
  128.                         else if (instructions[2] == 'R')
  129.                         {
  130.                             coordinates[0] = ballW + 1;
  131.                         }
  132.                         break;
  133.                     case 'B':
  134.                         coordinates[2] = ballD + 1;
  135.                         if (instructions[2] == 'L')
  136.                         {
  137.                             coordinates[0] = ballW - 1;
  138.                         }
  139.                         else if (instructions[2] == 'R')
  140.                         {
  141.                             coordinates[0] = ballW + 1;
  142.                         }
  143.                         break;
  144.                 }
  145.             }
  146.  
  147.             // handle the teleport T 1 1
  148.                
  149.             else if (instructions[0] == 'T')
  150.             {
  151.                 int[] instructionsTeleport = instructions.Substring(2).Split(' ').Select(x => Int32.Parse(x)).ToArray();
  152.                 coordinates[0] = instructionsTeleport[0];
  153.                 coordinates[1] = ballH;
  154.                 coordinates[2] = instructionsTeleport[1];
  155.             }
  156.             // handle empty cell
  157.             else if (instructions[0] == 'E')
  158.             {
  159.                 coordinates[0] = ballW;
  160.                 coordinates[1] = ballH + 1;
  161.                 coordinates[2] = ballD;
  162.             }
  163.  
  164.             return coordinates;
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement