Как вывести результат работы волнового алгоритма в строку

введите сюда описание изображения

введите сюда описание изображения

Суть задачи состоит в том чтобы записать хотя бы один кратчайший путь из точки 'S' в точку 'F' используя символы U(up), D(down), L(left), R(right). Есть у кого-то идеи, как можно это сделать чтобы работало пусть и не слишком эффективно. Код, вдруг понадобится: `

    static int[,] SearchAlgorithm(string PathToFile)
    {
        int width = GetWidth(PathToFile) + 2, height = GetHeight(PathToFile) + 2;
        int[,] Map = new int[height, width];
        int[,] t = GetMap(PathToFile);
        for(int i=0;i<height;i++)
        {
            for(int j=0;j<width;j++)
            {
                if (i == 0 || i == height - 1 || j == 0 || j == width - 1) 
                {
                    Map[i, j] = 1;
                }
                else
                {
                    try
                    {
                        Map[i, j] = t[i - 1, j - 1];
                    }
                    catch
                    {
                        Map[i, j] = t[i + 1, j + 1];
                    }
                }
            }
        } 

        bool add = true;
        int[,] CopyOfMap = new int[height, width];
        int step = 0;
        for (int i = 0; i < height; i++)
            for (int j = 0; j < width; j++)
            {
                if (Map[i, j] == 1)
                    CopyOfMap[i, j] = -2;
                else
                    CopyOfMap[i, j] = -1;
            }
        CopyOfMap[GetCoordinatesOfStart(PathToFile)[0] + 1, GetCoordinatesOfStart(PathToFile)[1] + 1] = 0;
        while (add == true)
        {
            add = false;
            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++)
                {
                    if (CopyOfMap[j, i] == step)
                    {
                        if (i - 1 >= 0 && CopyOfMap[j - 1, i] != -2 && CopyOfMap[j - 1, i] == -1)
                            CopyOfMap[j - 1, i] = step + 1;
                        if (j - 1 >= 0 && CopyOfMap[j, i - 1] != -2 && CopyOfMap[j, i - 1] == -1)
                            CopyOfMap[j, i - 1] = step + 1;
                        if (i + 1 < width && CopyOfMap[j + 1, i] != -2 && CopyOfMap[j + 1, i] == -1)
                            CopyOfMap[j + 1, i] = step + 1;
                        if (j + 1 < height && CopyOfMap[j, i + 1] != -2 && CopyOfMap[j, i + 1] == -1)
                            CopyOfMap[j, i + 1] = step + 1;
                    }
                }
            step++;
            add = true;
            if (CopyOfMap[GetCoordinatesOfEnd(PathToFile)[0] + 1, GetCoordinatesOfEnd(PathToFile)[1] + 1] != -1)
                add = false;
            if (step > width * height)
                add = false;
        }

        PrintMap(CopyOfMap, PathToFile);
        return CopyOfMap;
    }

    static string GetPathOrNoPath()
    {
        return "0";
    }
    static int[,] GetMap(string PathToFile)
    {
        int a, b;
        int[,] ArrayOfBalls;
        int[] CoordinatesOfStart = new int[2];
        int[] CoordinatesOfEnd = new int[2];
        using (StreamReader sr = new StreamReader(PathToFile))
        {
            a = Convert.ToInt32(sr.ReadLine());
            b = Convert.ToInt32(sr.ReadLine());
            ArrayOfBalls = new int[a, b];
            string t = sr.ReadLine();
            CoordinatesOfStart[0] = int.Parse(Convert.ToString(t[1])) - 1;
            CoordinatesOfStart[1] = int.Parse(Convert.ToString(t[3])) - 1;
            t = sr.ReadLine();
            CoordinatesOfEnd[0] = int.Parse(Convert.ToString(t[1])) - 1;
            CoordinatesOfEnd[1] = int.Parse(Convert.ToString(t[3])) - 1;
            string z = sr.ReadToEnd();
            int I, J;
            I = J = 0;
            for (int g = 0; g < z.Length; g++)
            {
                if (z[g] == '1')
                {
                    ArrayOfBalls[I, J++] = 1;
                    if (J >= a)
                    {
                        J = 0;
                        I++;
                    }
                }
                if (z[g] == '0')
                {
                    ArrayOfBalls[I, J++] = 0;
                    if (J >= a)
                    {
                        J = 0;
                        I++;
                    }
                }
            }
        }
        return ArrayOfBalls;
    } //!
    static int GetHeight(string PathToFile) 
    {
        using (StreamReader t = new StreamReader(PathToFile))
        {
            return Convert.ToInt32(t.ReadLine());
        }
    }
    static int GetWidth(string PathToFile) 
    {
        using (StreamReader t = new StreamReader(PathToFile))
        {
            t.ReadLine();
            return Convert.ToInt32(t.ReadLine());
        }
    }
    static int[] GetCoordinatesOfStart(string PathToFile) 
    {
        using (StreamReader t = new StreamReader(PathToFile))
        {
            int[] coordinates = new int[2];
            t.ReadLine();
            t.ReadLine(); 
            string str = t.ReadLine();
            coordinates[0] = int.Parse(Convert.ToString(str[1])) - 1;
            coordinates[1] = int.Parse(Convert.ToString(str[3])) - 1;
            return coordinates;
        }
    }
    static int[] GetCoordinatesOfEnd(string PathToFile) 
    {
        using (StreamReader t = new StreamReader(PathToFile))
        {
            int[] coordinates = new int[2];
            t.ReadLine();
            t.ReadLine();
            t.ReadLine();
            string str = t.ReadLine();
            coordinates[0] = int.Parse(Convert.ToString(str[1])) - 1;
            coordinates[1] = int.Parse(Convert.ToString(str[3])) - 1;
            return coordinates;
        }

    }
    static void PrintMap(int[,] a, string PathToFile)
    {
        for (int i = 0; i < GetHeight(PathToFile) + 2; i++) 
        {
            for (int j = 0; j < GetWidth(PathToFile) + 2; j++)
            {
                if (i == GetCoordinatesOfStart(PathToFile)[0] + 1 && j == GetCoordinatesOfStart(PathToFile)[1] + 1) Console.Write(" S| ");
                else if (i == GetCoordinatesOfEnd(PathToFile)[0] + 1 && j == GetCoordinatesOfEnd(PathToFile)[1] + 1) Console.Write(" F| ");
                else if (a[i, j] == -2)
                    Console.Write(" #| ");
                else if (a[i, j] == -1)
                    Console.Write("  | ");
                else
                    Console.Write(" " + a[i, j] + "| ");
            }
            Console.WriteLine();
        }
    }
}

} ` В методе "GetPathOrNoPath" хотелось бы реализовать как это сделать.


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