Advertisement
vic_alexiev

FormulaBit1.cs

Dec 29th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3.  
  4. class FormulaBit1
  5. {
  6.     private static bool IsInGrid(Point car, int gridSize)
  7.     {
  8.         return car.X >= 0 && car.X < gridSize
  9.             && car.Y >= 0 && car.Y < gridSize;
  10.     }
  11.  
  12.     private static bool IsOutThroughSouthWestCorner(Point car, int gridSize)
  13.     {
  14.         return car.X == gridSize - 1 && car.Y >= gridSize
  15.             || car.X >= gridSize && car.Y == gridSize - 1;
  16.     }
  17.  
  18.     static void Main()
  19.     {
  20.         int n = 8;
  21.  
  22.         int[,] grid = new int[n, n];
  23.  
  24.         for (int i = 0; i < n; i++)
  25.         {
  26.             string numberAsString = Console.ReadLine();
  27.             int number = Int32.Parse(numberAsString);
  28.  
  29.             for (int j = 0; j < n; j++)
  30.             {
  31.                 grid[i, j] = (number >> j) & 1;
  32.             }
  33.         }
  34.  
  35.         Point[] directions = new Point[4];
  36.         // South
  37.         directions[0] = new Point(1, 0);
  38.         // West
  39.         directions[1] = new Point(0, 1);
  40.         // North
  41.         directions[2] = new Point(-1, 0);
  42.         // West
  43.         directions[3] = new Point(0, 1);
  44.  
  45.         int trackLength = 0;
  46.         int turnsCount = 0;
  47.  
  48.         Point car = new Point(0, 0);
  49.  
  50.         Point currentDirection = directions[0];
  51.         int currentDirectionIndex = 0;
  52.  
  53.         // start looking for a track
  54.         while (true)
  55.         {
  56.             if (IsInGrid(car, n) && grid[car.X, car.Y] == 0)
  57.             {
  58.                 trackLength++;
  59.                 car.X += currentDirection.X;
  60.                 car.Y += currentDirection.Y;
  61.             }
  62.             else
  63.             {
  64.                 if (IsOutThroughSouthWestCorner(car, n))
  65.                 {
  66.                     Console.WriteLine("{0} {1}", trackLength, turnsCount);
  67.                     return;
  68.                 }
  69.  
  70.                 // return to previous position
  71.                 car.X -= currentDirection.X;
  72.                 car.Y -= currentDirection.Y;
  73.  
  74.                 // change direction
  75.                 currentDirectionIndex = (currentDirectionIndex + 1) % 4;
  76.                 currentDirection = directions[currentDirectionIndex];
  77.                 turnsCount++;
  78.  
  79.                 // move ahead
  80.                 car.X += currentDirection.X;
  81.                 car.Y += currentDirection.Y;
  82.  
  83.                 if (!IsInGrid(car, n) || grid[car.X, car.Y] == 1)
  84.                 {
  85.                     Console.WriteLine("No {0}", trackLength);
  86.                     return;
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement