Помогите сделать обход графа

Как сделать обход графа. Задание с CodeWars

You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return true if you can reach position [N-1, N-1] or false otherwise.

Empty positions are marked .. Walls are marked W. Start and exit positions are empty in all test cases.

Моё решение, что я делаю не так?

public class Kata
    {
        public static bool PathFinder(string maze)
        {
            bool result = false;

            var matrix = CreateMatrix(maze);

            var rows = matrix.GetLength(0);
            
            var columns = matrix.GetLength(1);

            var queue = new Queue<int>();

            var visited = new List<int>();

            queue.Enqueue(0);

            while(queue.Any())
            {
                var node = queue.Dequeue();

                int row = node/ rows,
                    column = node % columns;

                visited.Add(node);

                if (row == rows - 1 && column == columns - 1)
                {
                    result = true;
                    break;
                }
                else if(visited.Contains(node))
                {
                    continue;
                }
                else
                {
                    //Восток
                    if (columns > column + 1 && matrix[row, column + 1] == '.')
                    {
                        int next = row * rows + column + 1;

                        queue.Enqueue(next);
                    }

                    //Запад
                    if(column - 1 >= 0 && matrix[row, column-1] == '.')
                    {
                        int next = row * rows + column - 1;

                        queue.Enqueue(next);
                    }

                    //Юг
                    if(rows > row + 1 && matrix[row +1 , column] == '.')
                    {
                        int next = row + 1 * rows + column;

                        queue.Enqueue(next);
                    }

                    //Север
                    if(row - 1 >= 0 && matrix[row- 1, column] == '.')
                    {
                        int next = row - 1 * rows + column;

                        queue.Enqueue(next);
                    }
                }
            }

            return result;

        }

        private static char[,] CreateMatrix(string maze)
        {
            var mazeArray = maze.Split('\n');

            var matrix = new char[mazeArray.Length, mazeArray.Length];

            for(int i = 0; i<matrix.GetLength(0); i++)
            {
                for(int j = 0; j < matrix.GetLength(1); j++)
                {
                    matrix[i, j] = mazeArray[i][j];
                }
            }

            return matrix;
        }
    }

Ответы (0 шт):