Advertisement
Guest User

05. Knight Path

a guest
Mar 30th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         int[,] board = new int[8, 8];
  10.         int row = 0;
  11.         int col = 7;
  12.         board[0, 7] = 1;
  13.         int sum = 0;
  14.         bool checker = false;
  15.  
  16.  
  17.         while (true)
  18.         {
  19.             int backupCol = col;
  20.             int backupRow = row;
  21.             try
  22.             {
  23.  
  24.                 string[] command = Console.ReadLine().Split();
  25.                 if (command[0] == "stop")
  26.                 {
  27.                     break;
  28.                 }
  29.                 string directionX = command[0];
  30.                 string directionY = command[1];
  31.                 switch (directionX)
  32.                 {
  33.                     case "left": col -= 2; break;
  34.                     case "right": col += 2; break;
  35.                     case "up": row -= 2; break;
  36.                     case "down": row += 2; break;
  37.  
  38.                 }
  39.                 switch (directionY)
  40.                 {
  41.                     case "left": col -= 1; break;
  42.                     case "right": col += 1; break;
  43.                     case "up": row -= 1; break;
  44.                     case "down": row += 1; break;
  45.                 }
  46.                 if (board[row, col] == 1)
  47.                 {
  48.                     board[row, col] = 0;  
  49.                 }
  50.               else  
  51.                 {
  52.                     board[row, col] = 1;  
  53.                    
  54.                 }
  55.             }
  56.             catch (Exception)
  57.             {
  58.                 row = backupRow;
  59.                 col = backupCol;
  60.                 continue;
  61.             }
  62.         }
  63.         for (int i = 0; i < 8; i++)
  64.         {
  65.             for (int j = 0; j < 8; j++)
  66.             {
  67.                 sum += board[i, j];
  68.             }
  69.         }
  70.         if (sum ==0)
  71.         {
  72.             Console.WriteLine("[Board is empty]");
  73.             return;
  74.            
  75.         }
  76.  
  77.         for (int i = 0; i < 8; i++)
  78.         {
  79.             string result = "";
  80.  
  81.             for (int j = 0; j < 8; j++)
  82.             {
  83.                 result += "" + board[i, j];
  84.             }
  85.             int num = Convert.ToInt32(result, 2);
  86.             if (num > 0)
  87.             {
  88.                 checker = false;
  89.                 Console.WriteLine(num);
  90.             }
  91.         }
  92.  
  93.  
  94.  
  95.        
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement