Error System.StackOverflowException

0

I've been doing this code to make a maze and solve it. The first creates a vector of panels and labels, the panels are the squares of the board, and the text of the labels says if it is a wall (#), free path (+), path already crossed (-), the beginning (/ ), or the end or the exit (*).

    void generaTableroLaberinto()
    {
        /*Genera la cuadricula completa del laberinto, con un label con un # lo que significa que ese panel es un muro*/
        tablero = new Panel[tamañoLaberinto * tamañoLaberinto];
        indice = new Label[tamañoLaberinto * tamañoLaberinto];
        int a;

        for (int i = 0; i < tamañoLaberinto*tamañoLaberinto; i++)
        {
            a = azar.Next(1, 5);
            tablero[i] = new Panel();
            tablero[i].BackColor = Color.Beige;
            tablero[i].Width = 40;
            tablero[i].Height = 40;
            tablero[i].BorderStyle = BorderStyle.FixedSingle;

            //----------------------------------------------------

            indice[i] = new Label();

            /*Le da valores de # y + al laberinto al azar*/
            if (a == 1)
            {
                indice[i].Text = "#";
            }
            else
            {
                indice[i].Text = "+";
            }

            // indice[i].Text = (i).ToString();
            indice[i].Visible = true;
            indice[i].AutoSize = true;

            //----------------------------------------------------
            indice[i].Show();
            tablero[i].Show();
            tablero[i].Controls.Add(indice[i]);

            //Muestra el vector en forma de tablero----------------------------------------------------

            if (i == 0)
            {
                tablero[i].Location = new Point(20, 20);
                tablero[i].BackColor = Color.Orange;
                indice[i].Text = "/";
            }
            else
            {
                if(i % tamañoLaberinto == 0)
                {
                    tablero[i].Location = new Point(tablero[i-tamañoLaberinto].Location.X, tablero[i-tamañoLaberinto].Location.Y + 40);
                }
                else
                {
                    tablero[i].Location = new Point(tablero[i-1].Location.X + 40, tablero[i-1].Location.Y);
                }

            }
            if(i == (tamañoLaberinto*tamañoLaberinto) - 1)
            {
                tablero[i].BackColor = Color.Green;
                indice[i].Text = "*";
            }
            panel1.Controls.Add(tablero[i]);

        }

    }

And it goes through it until the end, the panels are red, but when executing this part the error "System.StackOverflowException" appears in the first if, and I do not know why

    void solucionLaberinto(int x1)
    { Boolean termina = false;
        int x2, k;
        k = 0;
        int[] mov = new int[5];
        mov[1] = 1;
        mov[2] = -1;
        mov[3] = tamañoLaberinto;
        mov[4] = -tamañoLaberinto;
        //-------------------------
        do
        {
            k = k + 1;
            x2 = x1 + mov[k];
            if (indice[x2].Text.Equals("+"))
            {
                //tablero[x2][y2] = s;
                tablero[x2].BackColor = Color.Red;
                if (indice[x2].Text != "*")
                {
                    solucionLaberinto(x2);
                    if (!termina)
                    {
                        indice[x2].Text = "-";
                    }
                }
                else
                {
                    termina = true;
                }
            }


        } while (k < 4 && !termina);

    }
    
asked by BerserkerVS 16.11.2018 в 02:09
source

1 answer

0

Your code looks a lot like a backtracking practice that I did in the race. I guess the shots are going around so I'll tell you where I think the fault is.

The problem I think is that you do not have any condition that evaluates if you have already passed through a box. In this way the program stays in a loop, always passing through them (in circles) until it gives a StackOverflow error. If you draw the code it should give you that the first execution moves to x1 + 1 and so on until you reach the width of the board. Then he explores x1-1 whereupon he goes back to exploring the path to the left where he has come from, which is incorrect.

The solution would be to pass a copy of the board in a recursive function parameter. I say this because the recursive function should also return true or false if it has reached the goal or if that path is cut off. Only if it returns true, you can red the path it has passed on the original board (correct solution). If you do it directly on the original board, an execution of one path will affect the execution of another and will not work.

The basic idea of backtracking is that a recursive call explore a path in depth and, if it is correct to indicate it. But if it is not the right one, it is unraveled until an earlier step where there is an unexplored path. And for that we use precisely the temporary variables that the stack gives us.

The stack also has a limited size so medium board sizes will give you a StackOverflow even if the code is correct. Therefore, if you are using this for artificial intelligence, it is better to use more modern search algorithms.

I hope I have helped you and I apologize in advance if my answer is not adequate.

    
answered by 21.11.2018 в 13:04