Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Runtime.CompilerServices;
- namespace Horse
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- private static char[,] matrix;
- private static bool[,] visited;
- private static Cell start;
- private static Cell end;
- //private Dictionary<int, List<int>> graph = new Dictionary<int, List<int>>();
- public static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- matrix = new char[n, n];
- visited = new bool[n,n];
- for (int row = 0; row < n; row++)
- {
- var line = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => x[0]).ToArray();
- for (int col = 0; col < line.Length; col++)
- {
- matrix[row, col] = line[col];
- if (line[col] == 'x')
- {
- visited[row, col] = true;
- }
- else if (line[col] == 's')
- {
- start = new Cell(row, col);
- }
- else if (line[col] == 'e')
- {
- end = new Cell(row, col);
- }
- }
- }
- var result = BFS(start, end);
- Console.WriteLine(result);
- }
- public static int BFS(Cell start, Cell end)
- {
- var queue = new Queue<Cell>();
- queue.Enqueue(start);
- visited[start.Row, start.Col] = true;
- while (queue.Count > 0)
- {
- var current = queue.Dequeue();
- if (current.Row == end.Row && current.Col == end.Col)
- {
- end.Count = current.Count;
- return end.Count;
- }
- var upLeft = new Cell(current.Row - 2, current.Col - 1, current.Count + 1);
- if (IsInMatrix(upLeft) && !visited[upLeft.Row, upLeft.Col])
- {
- queue.Enqueue(upLeft);
- visited[upLeft.Row, upLeft.Col] = true;
- }
- var leftUp = new Cell(current.Row - 1, current.Col - 2, current.Count + 1);
- if (IsInMatrix(leftUp) && !visited[leftUp.Row, leftUp.Col])
- {
- queue.Enqueue(leftUp);
- visited[leftUp.Row, leftUp.Col] = true;
- }
- var upRight = new Cell(current.Row - 2, current.Col + 1, current.Count + 1);
- if (IsInMatrix(upRight) && !visited[upRight.Row, upRight.Col])
- {
- queue.Enqueue(upRight);
- visited[upRight.Row, upRight.Col] = true;
- }
- var rightUp = new Cell(current.Row - 1, current.Col + 2, current.Count + 1);
- if (IsInMatrix(rightUp) && !visited[rightUp.Row, rightUp.Col])
- {
- queue.Enqueue(rightUp);
- visited[rightUp.Row, rightUp.Col] = true;
- }
- var downLeft = new Cell(current.Row + 2, current.Col - 1, current.Count + 1);
- if (IsInMatrix(downLeft) && !visited[downLeft.Row, downLeft.Col])
- {
- queue.Enqueue(downLeft);
- visited[downLeft.Row, downLeft.Col] = true;
- }
- var leftDown = new Cell(current.Row + 1, current.Col - 2, current.Count + 1);
- if (IsInMatrix(leftDown) && !visited[leftDown.Row, leftDown.Col])
- {
- queue.Enqueue(leftDown);
- visited[leftDown.Row, leftDown.Col] = true;
- }
- var downRight = new Cell(current.Row + 2, current.Col + 1, current.Count + 1);
- if (IsInMatrix(downRight) && !visited[downRight.Row, downRight.Col])
- {
- queue.Enqueue(downRight);
- visited[downRight.Row, downRight.Col] = true;
- }
- var rightDown = new Cell(current.Row + 1, current.Col + 2, current.Count + 1);
- if (IsInMatrix(rightDown) && !visited[rightDown.Row, rightDown.Col])
- {
- queue.Enqueue(rightDown);
- visited[rightDown.Row, rightDown.Col] = true;
- }
- }
- return -1;
- }
- public static bool IsInMatrix(Cell cell)
- {
- if (cell.Row < 0 || cell.Row >= matrix.GetLength(0) || cell.Col < 0 || cell.Col >= matrix.GetLength(1))
- {
- return false;
- }
- return true;
- }
- }
- public class Cell
- {
- public Cell(int row, int col, int count = 0)
- {
- this.Row = row;
- this.Col = col;
- this.Count = count;
- }
- public int Row { get; set; }
- public int Col { get; set; }
- public int Count { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement