Advertisement
d_brezoev

FormulaBit1-100

Nov 28th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FormulaBit1
  8. {
  9.     class FormulaBit1
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[,] matrix = new int[8, 8];
  14.             for (int i = 0; i < 8; i++)
  15.             {
  16.                 int number = int.Parse(Console.ReadLine());
  17.                 for (int k = 0; k < 8; k++)
  18.                 {
  19.                     int mask = 1 << k;
  20.                     int m = mask & number;
  21.                     int bit = m >> k;
  22.                     matrix[i, k] = bit;
  23.                 }
  24.             }
  25.             int row = 0;
  26.             int col = 0;
  27.             int turns = 0;
  28.             int steps = 0;
  29.             string direction = "down";
  30.             bool lastDirectionDown = true;
  31.             bool exitFound = false;
  32.             while (true)
  33.             {
  34.  
  35.                 if (matrix[row, col] == 1)
  36.                 {
  37.                     break;
  38.                 }
  39.                 steps++;
  40.                 if (row == 7 && col == 7)
  41.                 {
  42.                     exitFound = true;
  43.                     break;
  44.                 }
  45.                 if (direction == "down" && (row + 1 > 7 || matrix[row + 1, col] == 1))
  46.                 {
  47.                     direction = "left";
  48.                     if (col + 1 > 7 || matrix[row, col + 1] == 1)
  49.                     {
  50.                         break;
  51.                     }
  52.                     turns++;
  53.                 }
  54.                 else if (direction == "left" && (col + 1 > 7 || matrix[row, col + 1] == 1))
  55.                 {
  56.                     if (lastDirectionDown)
  57.                     {
  58.                         direction = "up";
  59.                         if (row - 1 < 0 || matrix[row - 1, col] == 1)
  60.                         {
  61.                             break;
  62.                         }
  63.                         turns++;
  64.                         lastDirectionDown = false;
  65.                     }
  66.                     else
  67.                     {
  68.                         direction = "down";
  69.                         if (row + 1 > 7 || matrix[row + 1, col] == 1)
  70.                         {
  71.                             break;
  72.                         }
  73.                         turns++;
  74.                         lastDirectionDown = true;
  75.                     }
  76.                 }
  77.                 else if (direction == "up" && (row - 1 < 0 || matrix[row - 1, col] == 1))
  78.                 {
  79.                     direction = "left";
  80.                     if (col + 1 > 7 || matrix[row, col + 1] == 1)
  81.                     {
  82.                         break;
  83.                     }
  84.                     turns++;
  85.                 }
  86.  
  87.                 if (direction == "down")
  88.                 {
  89.                     row++;
  90.                 }
  91.                 else if (direction == "up")
  92.                 {
  93.                     row--;
  94.                 }
  95.                 else if (direction == "left")
  96.                 {
  97.                     col++;
  98.                 }
  99.  
  100.             }
  101.             if (exitFound)
  102.             {
  103.                 Console.WriteLine(steps + " " + turns);
  104.             }
  105.             else
  106.             {
  107.                 Console.WriteLine("No " + steps);
  108.             }
  109.  
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement