Advertisement
sashomaga

FormulaBit1

Jan 5th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.99 KB | None | 0 0
  1. using System;
  2. //The residents of Bitlandia are huge sports fans. The bits have played almost every single sport that they have
  3. //learned from watching human TV i.e. EuroBitSport and BitTV. Today for the first time they watched Formula 1 and
  4. //now they certainly want to build a local track and start practicing right away. Of course the bits don’t want to
  5. //copy the humans. They want to be unique and therefore they’ve added some special rules:
  6. //1.    The track must be built on a grid of 8x8 cells, containing only zeros and ones.
  7. //2.    The track itself must contain only zeros. The width of the track will be one cell.
  8. //3.    The track must start from the upper right corner and end on the lower left corner.
  9. //4.    The cars can move only in 3 directions – South (down), West (left) And North (up).
  10. //5.    The first direction must always be south.
  11. //6.    The cars must move in the current direction, while it is possible i.e. the cars can turn only when it reaches
  12. //the end of the grid or a cell, containing a bit with a value of one.
  13. //7.    The cars can switch between directions only in the following order:
  14. //South -> West -> North -> West (and then again South -> West and so on)
  15. //You will receive information about the grid as a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7.
  16. //The grid itself is represented by the bits of those bytes.
  17. //Your task is to find whether a track can be built on the given grid. If the grid is appropriate, you should print the length
  18. //of the track and the count of the turns in it (the switches between directions), otherwise you should print “No” and the length
  19. //of the track until it was interrupted.
  20. //Input
  21. //The input data should be read from the console.
  22. //There will be exactly 8 lines each holding an integer number (n0, n1… n7).
  23. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  24. //Output
  25. //The output data should be printed on the console.
  26. //On the only output row you should print two numbers in the following format “X Y”, where X is the length of the track and Y is the
  27. //count of the turns. If a track cannot be built, you should print “No X”, where X is the length of the track, until it was interrupted.
  28. //Constraints
  29. //•   The numbers n0, n1, …, n7 are positive integers in the range [0…255]
  30. //•   Allowed work time for your program: 0.1 seconds.
  31. //•   Allowed memory: 16 MB.
  32.    
  33. //0 0   0   0   0   0   0   1   0   n0 = 2
  34. //1 0   0   1   0   0   1   1   0   n1 = 38
  35. //2 0   0   0   1   0   1   0   0   n2 = 20
  36. //3 0   0   1   1   0   0   0   0   n3 = 48
  37. //4 0   1   1   0   1   1   1   1   n4 = 111
  38. //5 0   0   0   0   1   1   1   1   n5 = 15
  39. //6 0   0   0   0   0   0   1   1   n6 = 3
  40. //7 0   0   1   0   1   0   1   1   n7 = 43
  41.            
  42. //0 0   0   0   0   0   0   1   0   n0 = 2
  43. //1 0   0   1   0   0   1   1   0   n1 = 38
  44. //2 0   0   0   1   1   1   0   0   n2 = 28
  45. //3 0   0   0   1   0   0   0   0   n3 = 16
  46. //4 0   1   0   0   0   1   1   1   n4 = 71
  47. //5 1   0   0   0   1   1   1   1   n5 = 143
  48. //6 0   0   0   0   0   0   1   1   n6 = 3
  49. //7 0   0   1   0   1   0   1   1   n7 = 43
  50.  
  51. //Example
  52. //input Example
  53. //2
  54. //38
  55. //20
  56. //48
  57. //111
  58. //15
  59. //3
  60. //43
  61. //output   
  62. //21 4
  63.  
  64. //Example
  65. //input Example
  66. //2
  67. //38
  68. //28
  69. //16
  70. //71
  71. //143
  72. //3
  73. //43   
  74. //output
  75. //No 7
  76.  
  77.  
  78. class Program
  79. {
  80.     static int[,] matrix = new int[8, 8];
  81.     static int carX = 0;
  82.     static int carY = matrix.GetLength(0)-1;
  83.     static int road = 1;
  84.     static int turn = -1;
  85.     static string lastDirection = "left";
  86.     static string lastVertDirection = "up";
  87.  
  88.     static void Main()
  89.     {
  90.         int[] input = new int[8];
  91.  
  92.  
  93.         for (int x = 0; x < input.Length; x++)
  94.         {
  95.             input[x] = int.Parse(Console.ReadLine());
  96.             for (int y = 0, reverse = input.Length - 1; y < input.Length; y++, reverse--)
  97.             {
  98.                 if ((input[x] >> y & 1) == 1)
  99.                 {
  100.                     matrix[x, reverse] = 1;
  101.                 }
  102.             }
  103.         }
  104.        
  105.         while (true)
  106.         {
  107.             while (Down())
  108.             {
  109.                 MoveDown();                        
  110.             }
  111.             while (Left())
  112.             {
  113.                 MoveLeft();
  114.             }
  115.             while (Up())
  116.             {
  117.                 MoveUp();
  118.             }
  119.             while (Left())
  120.             {
  121.                 MoveLeft();
  122.             }
  123.         }
  124.         //PrintMatrix(matrix);
  125.     }
  126.  
  127.     private static void MoveUp()
  128.     {
  129.         if (lastDirection == "left" && lastVertDirection == "down")
  130.         {
  131.             turn++;
  132.             lastVertDirection = "up";
  133.             lastDirection = "up";
  134.             road++;
  135.             carX--;
  136.         }
  137.         else if (lastDirection == "up")
  138.         {
  139.             road++;
  140.             carX--;
  141.         }
  142.         else
  143.         {
  144.             Lose();
  145.         }
  146.        
  147.         //Console.WriteLine("up");
  148.     }
  149.  
  150.     private static void MoveLeft()
  151.     {
  152.         if (lastDirection == "down" || lastDirection == "up")
  153.         {
  154.             turn++;
  155.             lastDirection = "left";
  156.             road++;
  157.             carY--;
  158.         }
  159.         else if (lastDirection == "left")
  160.         {
  161.             road++;
  162.             carY--;
  163.         }
  164.         else
  165.         {
  166.             Lose();
  167.         }
  168.        
  169.         //Console.WriteLine("left");
  170.     }
  171.  
  172.     private static void MoveDown()
  173.     {
  174.         if (lastDirection == "left" && lastVertDirection == "up")
  175.         {
  176.             turn++;
  177.             lastVertDirection = "down";
  178.             lastDirection = "down";
  179.             road++;
  180.             carX++;
  181.         }
  182.         else if (lastDirection == "down")
  183.         {
  184.             road++;
  185.             carX++;
  186.         }
  187.         else
  188.         {
  189.             Lose();
  190.         }
  191.         //Console.WriteLine("down");
  192.     }
  193.  
  194.     private static void Lose()
  195.     {
  196.         Console.WriteLine("No {0}",road);
  197.         Environment.Exit(0);
  198.     }
  199.  
  200.     private static bool Up()
  201.     {
  202.         if (carX != 0 && matrix[carX - 1,carY] == 0)
  203.         {
  204.             return true;
  205.         }
  206.         else
  207.         {
  208.             return false;
  209.         }
  210.     }
  211.  
  212.     private static bool Left()
  213.     {
  214.         if (carX == matrix.GetLength(0) - 1 && carY == 0)
  215.         {
  216.             Win();
  217.         }
  218.         if (carY != 0 && matrix[carX,carY - 1] == 0)
  219.         {
  220.             return true;
  221.         }
  222.         else
  223.         {
  224.             return false;
  225.         }
  226.     }
  227.  
  228.     private static bool Down()
  229.     {
  230.         if (carX == matrix.GetLength(0)-1 && carY == 0)
  231.         {
  232.             Win();
  233.         }
  234.         if (carX != matrix.GetLength(1) -1 && matrix[carX + 1,carY] == 0)
  235.         {
  236.             return true;
  237.         }
  238.         else
  239.         {
  240.             return false;
  241.         }
  242.  
  243.     }
  244.  
  245.     private static void Win()
  246.     {
  247.         Console.WriteLine(road+" "+turn);
  248.         Environment.Exit(0);
  249.     }
  250.  
  251.     private static void PrintMatrix(int[,] matrix)
  252.     {
  253.         for (int x = 0; x < matrix.GetLength(0); x++)
  254.         {
  255.             for (int y = 0; y < matrix.GetLength(1); y++)
  256.             {
  257.                 Console.Write(matrix[x, y]);
  258.             }
  259.             Console.WriteLine();
  260.         }
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement