vlad0

Sample Exam - 3D Lines

Feb 3rd, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _04._3D_Lines
  7. {
  8.     class Program
  9.     {
  10.         static char[, ,] cuboid;
  11.         static int maxLine = -1;
  12.         static int countMaxLine = 0;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string[] dimensions = Console.ReadLine().Split(' ');
  17.             //string[] dimensions = "4 3 5".Split(' ');
  18.             int width = int.Parse(dimensions[0]);
  19.             int height = int.Parse(dimensions[1]);
  20.             int depth = int.Parse(dimensions[2]);
  21.  
  22.             cuboid = new char[depth, height, width];
  23.             //string[] input = {
  24.             //                    "AAAA AAAA ABAA AAAB AABA",
  25.             //                    "AAAA BBBA AABA AABA ABAA",
  26.             //                    "BBBB ABBB ABBB ABAB BBAA",
  27.  
  28.             //                  };
  29.             FillCuboid(width, height, depth /*,input*/);
  30.             SameSliceRows();
  31.             SameSliceCols();
  32.             SameSliceUpDiagonal();
  33.             SameSliceDownDiagonal();
  34.  
  35.             DepthCheck();
  36.             DepthDownRightDiagonal();
  37.             DepthDownLeftDiagonal();
  38.             DepthUpRightDiagonal();
  39.             DepthUpLeftDiagonal();
  40.            
  41.             HorizontalDiagonal();
  42.             HorizontalUpWardDiagonal();
  43.  
  44.             DepthDiagonalColumn();
  45.             DepthDiagonalUpColumn();
  46.  
  47.             if (maxLine!=-1)
  48.             {
  49.                 Console.WriteLine("{0} {1}", maxLine, countMaxLine);    
  50.             }
  51.             else
  52.             {
  53.                 Console.WriteLine("-1");
  54.             }
  55.            
  56.  
  57.         }
  58.  
  59.         private static void DepthDiagonalUpColumn()
  60.         {
  61.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  62.             {
  63.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  64.                 {
  65.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  66.                     {
  67.                         char color = cuboid[slice, rows, cols];
  68.                         int currentCount = 0;
  69.                         for (int slicesCheck = slice, rowsCheck = rows;
  70.                             slicesCheck < cuboid.GetLength(0) && rowsCheck >=0;
  71.                             slicesCheck++, rowsCheck--)
  72.                         {
  73.                             if (cuboid[slicesCheck, rowsCheck,cols] == color)
  74.                             {
  75.                                 currentCount++;
  76.                             }
  77.                             else break;
  78.                         }
  79.  
  80.                         CheckMax(currentCount);
  81.  
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private static void DepthDiagonalColumn()
  88.         {
  89.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  90.             {
  91.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  92.                 {
  93.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  94.                     {
  95.                         char color = cuboid[slice, rows, cols];
  96.                         int currentCount = 0;
  97.                         for (int slicesCheck = slice, rowsCheck = rows;
  98.                             slicesCheck < cuboid.GetLength(0) && rowsCheck < cuboid.GetLength(1);
  99.                             slicesCheck++, rowsCheck++)
  100.                         {
  101.                             if (cuboid[slicesCheck, rowsCheck, cols] == color)
  102.                             {
  103.                                 currentCount++;
  104.                             }
  105.                             else break;
  106.                         }
  107.  
  108.                         CheckMax(currentCount);
  109.  
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.  
  115.         private static void DepthUpLeftDiagonal()
  116.         {
  117.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  118.             {
  119.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  120.                 {
  121.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  122.                     {
  123.                         char color = cuboid[slice, rows, cols];
  124.                         int currentCount = 0;
  125.                         for (int slicesCheck = slice, rowsCheck = rows, colsCheck = cols;
  126.                             slicesCheck < cuboid.GetLength(0) && colsCheck >= 0 && rowsCheck >= 0;
  127.                             slicesCheck++, rowsCheck--, colsCheck--)
  128.                         {
  129.                             if (cuboid[slicesCheck, rowsCheck, colsCheck] == color)
  130.                             {
  131.                                 currentCount++;
  132.                             }
  133.                             else break;
  134.                         }
  135.  
  136.                         CheckMax(currentCount);
  137.  
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.         private static void DepthUpRightDiagonal()
  144.         {
  145.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  146.             {
  147.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  148.                 {
  149.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  150.                     {
  151.                         char color = cuboid[slice, rows, cols];
  152.                         int currentCount = 0;
  153.                         for (int slicesCheck = slice, rowsCheck = rows, colsCheck = cols;
  154.                             slicesCheck < cuboid.GetLength(0) && colsCheck < cuboid.GetLength(2) && rowsCheck >=0;
  155.                             slicesCheck++, rowsCheck--, colsCheck++)
  156.                         {
  157.                             if (cuboid[slicesCheck, rowsCheck, colsCheck] == color)
  158.                             {
  159.                                 currentCount++;
  160.                             }
  161.                             else break;
  162.                         }
  163.  
  164.                         CheckMax(currentCount);
  165.  
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.  
  171.  
  172.         private static void HorizontalUpWardDiagonal()
  173.         {
  174.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  175.             {
  176.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  177.                 {
  178.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  179.                     {
  180.                         char color = cuboid[slice, rows, cols];
  181.                         int currentCount = 0;
  182.                         for (int slicesCheck = slice, colsCheck = cols;
  183.                             slicesCheck < cuboid.GetLength(0) && colsCheck < cuboid.GetLength(2);
  184.                             slicesCheck++, colsCheck++)
  185.                         {
  186.                             if (cuboid[slicesCheck, rows, colsCheck] == color)
  187.                             {
  188.                                 currentCount++;
  189.                             }
  190.                             else break;
  191.                         }
  192.  
  193.                         CheckMax(currentCount);
  194.  
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.  
  200.         private static void HorizontalDiagonal()
  201.         {
  202.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  203.             {
  204.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  205.                 {
  206.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  207.                     {
  208.                         char color = cuboid[slice, rows, cols];
  209.                         int currentCount = 0;
  210.                         for (int slicesCheck = slice, colsCheck = cols;
  211.                             slicesCheck < cuboid.GetLength(0) && colsCheck >= 0;
  212.                             slicesCheck++, colsCheck--)
  213.                         {
  214.                             if (cuboid[slicesCheck, rows, colsCheck] == color)
  215.                             {
  216.                                 currentCount++;
  217.                             }
  218.                             else break;
  219.                         }
  220.  
  221.                         CheckMax(currentCount);
  222.  
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.  
  228.         private static void DepthDownRightDiagonal()
  229.         {
  230.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  231.             {
  232.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  233.                 {
  234.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  235.                     {
  236.                         char color = cuboid[slice, rows, cols];
  237.                         int currentCount = 0;
  238.                         for (int slicesCheck = slice, rowsCheck = rows, colsCheck = cols;
  239.                             slicesCheck < cuboid.GetLength(0) && colsCheck < cuboid.GetLength(2) && rowsCheck < cuboid.GetLength(1);
  240.                             slicesCheck++, rowsCheck++, colsCheck++)
  241.                         {
  242.                             if (cuboid[slicesCheck, rowsCheck, colsCheck] == color)
  243.                             {
  244.                                 currentCount++;
  245.                             }
  246.                             else break;
  247.                         }
  248.  
  249.                         CheckMax(currentCount);
  250.  
  251.                     }
  252.                 }
  253.             }
  254.         }
  255.  
  256.         private static void DepthDownLeftDiagonal()
  257.         {
  258.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  259.             {
  260.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  261.                 {
  262.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  263.                     {
  264.                         char color = cuboid[slice, rows, cols];
  265.                         int currentCount = 0;
  266.                         for (int slicesCheck = slice, rowsCheck = rows,colsCheck = cols;
  267.                             slicesCheck < cuboid.GetLength(0) && colsCheck>=0 && rowsCheck < cuboid.GetLength(1);
  268.                             slicesCheck++,rowsCheck++,colsCheck--)
  269.                         {
  270.                             if (cuboid[slicesCheck, rowsCheck, colsCheck] == color)
  271.                             {
  272.                                 currentCount++;
  273.                             }
  274.                             else break;
  275.                         }
  276.  
  277.                         CheckMax(currentCount);
  278.  
  279.                     }
  280.                 }
  281.             }
  282.         }
  283.  
  284.         private static void SameSliceDownDiagonal()
  285.         {
  286.             for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  287.             {
  288.                 for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  289.                 {
  290.                     for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  291.                     {
  292.                         char color = cuboid[slice, rows, cols];
  293.                         int currentCount = 0;
  294.                         for (int colsCheck = cols, rowsCheck = rows;
  295.                             colsCheck < cuboid.GetLength(2) && rowsCheck < cuboid.GetLength(1);
  296.                             colsCheck++, rowsCheck++)
  297.                         {
  298.                             if (cuboid[slice, rowsCheck, colsCheck] == color)
  299.                             {
  300.                                 currentCount++;
  301.                             }
  302.                             else break;
  303.                         }
  304.  
  305.                         CheckMax(currentCount);
  306.                     }
  307.                 }
  308.             }
  309.         }
  310.  
  311.         private static void SameSliceUpDiagonal()
  312.         {
  313.             for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  314.             {
  315.                 for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  316.                 {
  317.                     for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  318.                     {
  319.                         char color = cuboid[slice, rows, cols];
  320.                         int currentCount = 0;
  321.                         for (int colsCheck = cols, rowsCheck = rows;
  322.                             colsCheck < cuboid.GetLength(2) && rowsCheck>=0;
  323.                             colsCheck++,rowsCheck--)
  324.                         {
  325.                             if (cuboid[slice, rowsCheck, colsCheck] == color)
  326.                             {
  327.                                 currentCount++;
  328.                             }
  329.                             else break;
  330.                         }
  331.  
  332.                         CheckMax(currentCount);
  333.  
  334.                     }
  335.                 }
  336.             }
  337.         }
  338.  
  339.         private static void DepthCheck()
  340.         {
  341.  
  342.             for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  343.             {
  344.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  345.                 {
  346.                     for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  347.                     {
  348.                         char color = cuboid[slice, rows, cols];
  349.                         int currentCount = 0;
  350.                         for (int slicesCheck = slice; slicesCheck < cuboid.GetLength(0); slicesCheck++)
  351.                         {
  352.                             if (cuboid[slicesCheck, rows, cols] == color)
  353.                             {
  354.                                 currentCount++;
  355.                             }
  356.                             else break;
  357.                         }
  358.  
  359.                         CheckMax(currentCount);
  360.  
  361.                     }
  362.                 }
  363.             }
  364.         }
  365.  
  366.         private static void SameSliceCols()
  367.         {
  368.             for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  369.             {
  370.                 for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  371.                 {
  372.                     for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  373.                     {
  374.                         char color = cuboid[slice, rows, cols];
  375.                         int currentCount = 0;
  376.                         for (int rowsCheck = rows; rowsCheck < cuboid.GetLength(1); rowsCheck++)
  377.                         {
  378.                             if (cuboid[slice, rowsCheck, cols] == color)
  379.                             {
  380.                                 currentCount++;
  381.                             }
  382.                             else break;
  383.                         }
  384.  
  385.                         CheckMax(currentCount);
  386.  
  387.                     }
  388.                 }
  389.             }
  390.         }
  391.  
  392.         private static void SameSliceRows()
  393.         {
  394.             for (int slice = 0; slice < cuboid.GetLength(0); slice++)
  395.             {
  396.                 for (int rows = 0; rows < cuboid.GetLength(1); rows++)
  397.                 {
  398.                     for (int cols = 0; cols < cuboid.GetLength(2); cols++)
  399.                     {
  400.                         char color = cuboid[slice, rows, cols];
  401.                         int currentCount = 0;
  402.                         for (int colsCheck = cols; colsCheck < cuboid.GetLength(2); colsCheck++)
  403.                         {
  404.                             if (cuboid[slice, rows, colsCheck] == color)
  405.                             {
  406.                                 currentCount++;
  407.                             }
  408.                             else break;
  409.                         }
  410.  
  411.                         CheckMax(currentCount);
  412.  
  413.                     }
  414.                 }
  415.             }
  416.         }
  417.  
  418.         private static void CheckMax(int currentCount)
  419.         {
  420.             if (currentCount > maxLine && currentCount>1)
  421.             {
  422.                 maxLine = currentCount;
  423.                 countMaxLine = 1;
  424.             }
  425.             else if (currentCount == maxLine)
  426.             {
  427.                 countMaxLine++;
  428.             }
  429.         }
  430.  
  431.         private static void FillCuboid(int width, int height, int depth/*, string[] input*/)
  432.         {
  433.             for (int rows = 0; rows < height; rows++)
  434.             {
  435.                 //string[] line = input[rows].Split(' ');
  436.                 string[] line = Console.ReadLine().Split(' ');
  437.                 int lineLength = line.Length;
  438.  
  439.                 for (int slices = 0; slices < lineLength; slices++)
  440.                 {
  441.                     int slicesLength = line[slices].Length;
  442.  
  443.                     for (int cols = 0; cols < slicesLength; cols++)
  444.                     {
  445.                         cuboid[slices, rows, cols] = line[slices][cols];
  446.                     }
  447.                 }
  448.  
  449.             }
  450.         }
  451.     }
  452. }
Add Comment
Please, Sign In to add comment