Advertisement
svetlai

Horse

Dec 1st, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System.Runtime.CompilerServices;
  2.  
  3. namespace Horse
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Linq;
  8.     using System.Text;
  9.     using System.Threading.Tasks;
  10.  
  11.     public class Program
  12.     {
  13.         private static char[,] matrix;
  14.         private static bool[,] visited;
  15.         private static Cell start;
  16.         private static Cell end;
  17.  
  18.         //private Dictionary<int, List<int>> graph = new Dictionary<int, List<int>>();
  19.         public static void Main(string[] args)
  20.         {
  21.             int n = int.Parse(Console.ReadLine());
  22.             matrix = new char[n, n];
  23.             visited = new bool[n,n];
  24.             for (int row = 0; row < n; row++)
  25.             {
  26.                 var line = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => x[0]).ToArray();
  27.                 for (int col = 0; col < line.Length; col++)
  28.                 {
  29.                     matrix[row, col] = line[col];
  30.  
  31.                     if (line[col] == 'x')
  32.                     {
  33.                         visited[row, col] = true;
  34.                     }
  35.                     else if (line[col] == 's')
  36.                     {
  37.                      start = new Cell(row, col);
  38.                     }
  39.                     else if (line[col] == 'e')
  40.                     {
  41.                         end = new Cell(row, col);
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             var result = BFS(start, end);
  47.             Console.WriteLine(result);
  48.         }
  49.  
  50.         public static int BFS(Cell start, Cell end)
  51.         {
  52.             var queue = new Queue<Cell>();
  53.             queue.Enqueue(start);
  54.             visited[start.Row, start.Col] = true;
  55.  
  56.             while (queue.Count > 0)
  57.             {
  58.                 var current = queue.Dequeue();
  59.  
  60.                 if (current.Row == end.Row && current.Col == end.Col)
  61.                 {
  62.                     end.Count = current.Count;
  63.                     return end.Count;
  64.                 }
  65.  
  66.                 var upLeft = new Cell(current.Row - 2, current.Col - 1, current.Count + 1);
  67.                 if (IsInMatrix(upLeft) && !visited[upLeft.Row, upLeft.Col])
  68.                 {
  69.                     queue.Enqueue(upLeft);
  70.                     visited[upLeft.Row, upLeft.Col] = true;
  71.                 }
  72.  
  73.                 var leftUp = new Cell(current.Row - 1, current.Col - 2, current.Count + 1);
  74.                 if (IsInMatrix(leftUp) && !visited[leftUp.Row, leftUp.Col])
  75.                 {
  76.                     queue.Enqueue(leftUp);
  77.                     visited[leftUp.Row, leftUp.Col] = true;
  78.                 }
  79.  
  80.                 var upRight = new Cell(current.Row - 2, current.Col + 1, current.Count + 1);
  81.                 if (IsInMatrix(upRight) && !visited[upRight.Row, upRight.Col])
  82.                 {
  83.                     queue.Enqueue(upRight);
  84.                     visited[upRight.Row, upRight.Col] = true;
  85.                 }
  86.  
  87.                 var rightUp = new Cell(current.Row - 1, current.Col + 2, current.Count + 1);
  88.                 if (IsInMatrix(rightUp) && !visited[rightUp.Row, rightUp.Col])
  89.                 {
  90.                     queue.Enqueue(rightUp);
  91.                     visited[rightUp.Row, rightUp.Col] = true;
  92.                 }
  93.  
  94.                 var downLeft = new Cell(current.Row + 2, current.Col - 1, current.Count + 1);
  95.                 if (IsInMatrix(downLeft) && !visited[downLeft.Row, downLeft.Col])
  96.                 {
  97.                     queue.Enqueue(downLeft);
  98.                     visited[downLeft.Row, downLeft.Col] = true;
  99.                 }
  100.  
  101.                 var leftDown = new Cell(current.Row + 1, current.Col - 2, current.Count + 1);
  102.                 if (IsInMatrix(leftDown) && !visited[leftDown.Row, leftDown.Col])
  103.                 {
  104.                     queue.Enqueue(leftDown);
  105.                     visited[leftDown.Row, leftDown.Col] = true;
  106.                 }
  107.  
  108.                 var downRight = new Cell(current.Row + 2, current.Col + 1, current.Count + 1);
  109.                 if (IsInMatrix(downRight) && !visited[downRight.Row, downRight.Col])
  110.                 {
  111.                     queue.Enqueue(downRight);
  112.                     visited[downRight.Row, downRight.Col] = true;
  113.                 }
  114.  
  115.                 var rightDown = new Cell(current.Row + 1, current.Col + 2, current.Count + 1);
  116.                 if (IsInMatrix(rightDown) && !visited[rightDown.Row, rightDown.Col])
  117.                 {
  118.                     queue.Enqueue(rightDown);
  119.                     visited[rightDown.Row, rightDown.Col] = true;
  120.                 }
  121.             }
  122.  
  123.             return -1;
  124.         }
  125.  
  126.         public static bool IsInMatrix(Cell cell)
  127.         {
  128.             if (cell.Row < 0 || cell.Row >= matrix.GetLength(0) || cell.Col < 0 || cell.Col >= matrix.GetLength(1))
  129.             {
  130.                 return false;
  131.             }
  132.  
  133.             return true;
  134.         }
  135.     }
  136.  
  137.     public class Cell
  138.     {
  139.         public Cell(int row, int col, int count = 0)
  140.         {
  141.             this.Row = row;
  142.             this.Col = col;
  143.             this.Count = count;
  144.         }
  145.  
  146.         public int Row { get; set; }
  147.  
  148.         public int Col { get; set; }
  149.  
  150.         public int Count { get; set; }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement