stanevplamen

02.09.01.03.Slides

Jun 19th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.35 KB | None | 0 0
  1. using System;
  2.  
  3. class Slides
  4. {
  5.     static int[] mark = new int[3];
  6.  
  7.     static void Main()
  8.     {
  9.         string cubeInput = Console.ReadLine(); // "3 3 3"; //
  10.         string[] splitedArray = cubeInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  11.  
  12.         int width = int.Parse(splitedArray[0]);
  13.         int heigth = int.Parse(splitedArray[1]);
  14.         int depth = int.Parse(splitedArray[2]);
  15.         string[] inputs = new string[heigth]; //
  16.         for (int i = 0; i < heigth; i++)
  17.         {
  18.             inputs[i] = Console.ReadLine();
  19.         }
  20.  
  21.         string start = Console.ReadLine(); // "1 1"; //
  22.         string[] startPosition = start.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  23.         int startWidth = int.Parse(startPosition[0]);
  24.         int startDepth = int.Parse(startPosition[1]);
  25.         int startHeigth = 0;
  26.  
  27.  
  28.         string[, ,] theMatrix = LoadTheMatrix(inputs, width, heigth, depth);
  29.  
  30.         FindPathToExit(theMatrix, startWidth, startHeigth, startDepth);
  31.     }
  32.  
  33.     static string[, ,] LoadTheMatrix(string[] inputs, int width, int heigth, int depth)
  34.     {
  35.         string[, ,] theMatrix = new string[width, heigth, depth];
  36.         for (int h = 0; h < heigth; h++)
  37.         {
  38.             string[] splitedH = inputs[h].Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  39.             for (int d = 0; d < depth; d++)
  40.             {
  41.                 string[] splitedD = splitedH[d].Trim().Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
  42.                 for (int w = 0; w < width; w++)
  43.                 {
  44.                     theMatrix[w, h, d] = splitedD[w];
  45.                 }
  46.             }
  47.         }
  48.         return theMatrix;
  49.     }
  50.  
  51.     static void FindPathToExit(string[, ,] theMatrix, int widthCurrent, int heigthCurrent, int depthCurrent)
  52.     {
  53.  
  54.         // следват две проверки дали не сме задънили
  55.         if (!InRange(theMatrix, widthCurrent, depthCurrent))
  56.         {
  57.             Console.WriteLine("No\n{0} {1} {2}", mark[0], mark[1], mark[2]);
  58.             Environment.Exit(0);
  59.         }
  60.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "B")
  61.         {
  62.             Console.WriteLine("No\n{0} {1} {2}", widthCurrent, heigthCurrent, depthCurrent);
  63.             Environment.Exit(0);
  64.         }
  65.        
  66.         //  проверяване дали не сме намерили изход
  67.         if (Exit(theMatrix, heigthCurrent) &&
  68.             (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S L" ||
  69.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S R" ||
  70.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S B" ||
  71.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S F" ||
  72.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S FL" ||
  73.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S FR" ||
  74.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S BL" ||
  75.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S BR" ||
  76.             theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "E" ))
  77.         {
  78.             Console.WriteLine("Yes\n{0} {1} {2}", widthCurrent, heigthCurrent, depthCurrent);
  79.             Environment.Exit(0);
  80.         }
  81.  
  82.         // следва проветка дали няма директно задаване на ново координати
  83.         string transCheck = theMatrix[widthCurrent, heigthCurrent, depthCurrent];
  84.         string[] splitedTrans = transCheck.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  85.  
  86.         if (splitedTrans[0].Trim() == "T")
  87.         {
  88.             //heigthCurrent = heigthCurrent;
  89.             widthCurrent = int.Parse(splitedTrans[1].Trim());
  90.             depthCurrent = int.Parse(splitedTrans[2].Trim());
  91.             FindPathToExit(theMatrix, widthCurrent, heigthCurrent, depthCurrent);
  92.         }
  93.  
  94.  
  95.         mark[0] = widthCurrent; mark[1] = heigthCurrent; mark[2] = depthCurrent;
  96.  
  97.         // следва проверка ако вървим надолу - влизаме в рекурсия
  98.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "E")
  99.         {
  100.             FindPathToExit(theMatrix, widthCurrent, heigthCurrent + 1, depthCurrent);
  101.         }
  102.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S R")
  103.         {
  104.             FindPathToExit(theMatrix, widthCurrent + 1, heigthCurrent + 1, depthCurrent);
  105.         }
  106.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S L")
  107.         {
  108.             FindPathToExit(theMatrix, widthCurrent - 1, heigthCurrent + 1, depthCurrent);
  109.         }
  110.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S B")
  111.         {
  112.             FindPathToExit(theMatrix, widthCurrent, heigthCurrent + 1, depthCurrent + 1);
  113.         }
  114.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S F")
  115.         {
  116.             FindPathToExit(theMatrix, widthCurrent, heigthCurrent + 1, depthCurrent - 1);
  117.         }
  118.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S FL")
  119.         {
  120.             FindPathToExit(theMatrix, widthCurrent - 1, heigthCurrent + 1, depthCurrent - 1);
  121.         }
  122.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S FR")
  123.         {
  124.             FindPathToExit(theMatrix, widthCurrent + 1, heigthCurrent + 1, depthCurrent - 1);
  125.         }
  126.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S BL")
  127.         {
  128.             FindPathToExit(theMatrix, widthCurrent - 1, heigthCurrent + 1, depthCurrent + 1);
  129.         }
  130.         if (theMatrix[widthCurrent, heigthCurrent, depthCurrent] == "S BR")
  131.         {
  132.             FindPathToExit(theMatrix, widthCurrent + 1, heigthCurrent + 1, depthCurrent + 1);
  133.         }
  134.  
  135.  
  136.     }
  137.     static bool Exit(string[, ,] theMatrix, int heigthCurrent)
  138.     {
  139.         bool heigtInRange = heigthCurrent >= theMatrix.GetLength(1) - 1;
  140.         return heigtInRange;
  141.     }
  142.  
  143.     static bool InRange(string[, ,] theMatrix, int widthCurrent, int depthCurrent)
  144.     {
  145.         bool widthInRange = widthCurrent >= 0 && widthCurrent < theMatrix.GetLength(0);
  146.         bool depthInRange = depthCurrent >= 0 && depthCurrent < theMatrix.GetLength(2);
  147.         return widthInRange && depthInRange;
  148.     }
  149. }
Add Comment
Please, Sign In to add comment