vkv1986

FormulaBit

Jan 9th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.24 KB | None | 0 0
  1. using System;
  2.  
  3. class FormulaBit
  4. {
  5.     static void Main()
  6.     {
  7.         byte[] number = new byte[8];
  8.         for (int position = 0; position < 8; position++)
  9.         {
  10.             number[position] = byte.Parse(Console.ReadLine());
  11.         }
  12.         int path = 0;
  13.         int turns = 0;
  14.         bool isWest = false;
  15.         bool isSouth = true;
  16.         bool noPath = false;
  17.         byte row = 0;
  18.         byte column = 0;
  19.         byte bitSouth = 0;
  20.         byte bitWest = 0;
  21.         byte bitNorth = 0;
  22.         int mask = 1 << column;
  23.         int maskNumber = mask & number[row];
  24.         bitSouth = (byte)(maskNumber >> column);
  25.         if (bitSouth == 0)
  26.         {
  27.             path++;
  28.         }
  29.         else
  30.         {
  31.             noPath = true;
  32.         }
  33.         while (!noPath && (row < 7 || column < 7))
  34.         {
  35.             if (isSouth && !isWest)
  36.             {
  37.                 if (row < 7)
  38.                 {
  39.                     int maskSouth = 1 << column;
  40.                     int maskNumberSouth = maskSouth & number[row + 1];
  41.                     bitSouth = (byte)(maskNumberSouth >> column);
  42.                 }
  43.                 else
  44.                 {
  45.                     bitSouth = (byte)1;
  46.                 }
  47.                 if (column < 7)
  48.                 {
  49.                     int maskWest = 1 << column + 1;
  50.                     int maskNumberWest = maskWest & number[row];
  51.                     bitWest = (byte)(maskNumberWest >> column + 1);
  52.                 }
  53.                 else
  54.                 {
  55.                     bitWest = (byte)1;
  56.                 }
  57.                 if (bitSouth == 0)
  58.                 {
  59.                     row++;
  60.                     path++;
  61.                 }
  62.                 else if (bitWest == 0)
  63.                 {
  64.                     column++;
  65.                     path++;
  66.                     turns++;
  67.                     isWest = true;
  68.                 }
  69.                 else
  70.                 {
  71.                     noPath = true;
  72.                 }
  73.             }
  74.             else if (!isSouth && !isWest)
  75.             {
  76.                 if (row > 0)
  77.                 {
  78.                     int maskNorth = 1 << column;
  79.                     int maskNumberNorth = maskNorth & number[row - 1];
  80.                     bitNorth = (byte)(maskNumberNorth >> column);
  81.                 }
  82.                 else
  83.                 {
  84.                     bitNorth = (byte)1;
  85.                 }
  86.                 if (column < 7)
  87.                 {
  88.                     int maskWest = 1 << column + 1;
  89.                     int maskNumberWest = maskWest & number[row];
  90.                     bitWest = (byte)(maskNumberWest >> column + 1);
  91.                 }
  92.                 else
  93.                 {
  94.                     bitWest = (byte)1;
  95.                 }
  96.                 if (bitNorth == 0)
  97.                 {
  98.                     row--;
  99.                     path++;
  100.                 }
  101.                 else if (bitWest == 0)
  102.                 {
  103.                     column++;
  104.                     path++;
  105.                     turns++;
  106.                     isWest = true;
  107.                 }
  108.                 else
  109.                 {
  110.                     noPath = true;
  111.                 }
  112.             }
  113.             else if (isWest)
  114.             {
  115.                 if (row < 7)
  116.                 {
  117.                     int maskSouth = 1 << column;
  118.                     int maskNumberSouth = maskSouth & number[row + 1];
  119.                     bitSouth = (byte)(maskNumberSouth >> column);
  120.                 }
  121.                 else
  122.                 {
  123.                     bitSouth = (byte)1;
  124.                 }
  125.                 if (row > 0)
  126.                 {
  127.                     int maskNorth = 1 << column;
  128.                     int maskNumberNorth = maskNorth & number[row - 1];
  129.                     bitNorth = (byte)(maskNumberNorth >> column);
  130.                 }
  131.                 else
  132.                 {
  133.                     bitNorth = (byte)1;
  134.                 }
  135.                 if (column < 7)
  136.                 {
  137.                     int maskWest = 1 << column + 1;
  138.                     int maskNumberWest = maskWest & number[row];
  139.                     bitWest = (byte)(maskNumberWest >> column + 1);
  140.                 }
  141.                 else
  142.                 {
  143.                     bitWest = (byte)1;
  144.                 }
  145.                 if (bitWest == 0)
  146.                 {
  147.                     column++;
  148.                     path++;
  149.                 }
  150.                 else if (bitSouth == 0 && !isSouth)
  151.                 {
  152.                     row++;
  153.                     path++;
  154.                     turns++;
  155.                     isSouth = true;
  156.                     isWest = false;
  157.                 }
  158.                 else if (bitNorth == 0 && isSouth)
  159.                 {
  160.                     row--;
  161.                     path++;
  162.                     turns++;
  163.                     isSouth = false;
  164.                     isWest = false;
  165.                 }
  166.                 else
  167.                 {
  168.                     noPath = true;
  169.                 }
  170.             }
  171.         }
  172.         if (row == 7 && column == 7)
  173.         {
  174.             Console.WriteLine("{0} {1}", path, turns);
  175.         }
  176.         else
  177.         {
  178.             Console.WriteLine("No {0}", path);
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment