C # recursive function stack splits a variable from nowhere

I tried to create a program that finds a Hamilton track in a maze using reverse tracking. It must return the path in a maze encoded in numbers. The problem is that when the stack crashes, one of the variables (which is a representation of the maze) is inherited from the call, and the others, even if they are declared the same way, do not (this is normal). I tried several workarounds, including instantiating, creating a separate class, and I turned on debugging messages. Here is the code with some comments to help.

using System;


namespace ConsoleApplication1
{
    //I made a separate class for the function
    class btr
    {
        public short[,] mz = new short[,] { };//tried to pull the variable out of the function, no success 
        public void bt(int i, int j, int l)
        {
            bool ok;
            ok = true;
            Console.WriteLine("in" + '\n' + Program.print(mz, l) + 'i' + i + 'j' + j + '\n'); //debug message for entering
            if (i > 0 && mz[i - 1, j] == 0)
            {
                ok = false;
                mz[i, j] = 1; // 1 aka go up
                var x = new btr { };
                //my attempt to avoid the problem by instantiating the function, no success...
                x.mz = mz;
                x.bt(i - 1, j, l);
                //When this function exits the mz variable is copied to this one. Same for all the ifs below
            }
            if (j > 0 && mz[i, j - 1] == 0)
            {
                ok = false;
                mz[i, j] = 2; //2 aka go left
                var x = new btr { };
                x.mz = mz;
                x.bt(i, j - 1, l);
            }
            if (i < l && mz[i + 1, j] == 0)
            {
                ok = false;
                mz[i, j] = 3;//3 aka go down 
                var x = new btr { };
                x.mz = mz;
                x.bt(i + 1, j, l);
            }
            if (j < l && mz[i, j + 1] == 0)
            {
                ok = false;
                mz[i, j] = 4;//4 aka go right 
                var x = new btr { };
                x.mz = mz;
                x.bt(i, j + 1, l);
            }
            Console.WriteLine("out" + '\n' + Program.print(mz, l) + 'i' + i + 'j' + j + '\n');  //debug message for exiting
            if (ok) //this is for printing the solution when it is found
            {
                mz[i, j] = 8;// 8 aka the end
                foreach (int x in mz)
                {
                    if (x == 0) { ok = false; break; }
                }
                if (ok)
                    Console.WriteLine("result" + '\n' + Program.print(mz, l));
            }
        }
    }
    class Program
    {//this is just for preparing the first call 

    static short[,] test = new short[2, 2] { { 0, 0}, { 0, 0} };

    static void Main(string[] args)
    {
        var x= new btr { };
        x.mz = test;
        x.bt(0,0,1);
    }
    public static string print(short[,] vr,int l)//casts my array into a string that can be printed
    {
        string s = "";
        for (int i = 0; i <= l; i++)
        {
            for (int j = 0; j <= l; j++)
            {
                s += vr[i,j];
            }
            s += '\n';
        }
        return s;
    }
}

}

2x2 - ( , 0), 2 , , "" . :

in
00
00
i0j0

in
30
00
i1j0

in
30
40
i1j1

in
30
41
i0j1

out
30
41
i0j1

result
38
41

out
38
41
i1j1

out
38
41
i1j0

out
38
41
i0j0

, , 38 41 00 00, . j .

+4
1

, , , . , , .

, , , , reset . β€” i j , β€” .

:

public void bt(int i, int j, int l)
{
    bool ok;
    ok = true;
    Console.WriteLine("in" + '\n' + Program.print(mz, l) + 'i' + i + 'j' + j + '\n'); //debug message for entering
    if (i > 0 && mz[i - 1, j] == 0)
    {
        ok = false;
        mz[i, j] = 1; // 1 aka go up
        bt(i - 1, j, l);
        mz[i, j] = 0;
        //When this function exits the mz variable is copied to this one. Same for all the ifs below
    }
    if (j > 0 && mz[i, j - 1] == 0)
    {
        ok = false;
        mz[i, j] = 2; //2 aka go left
        bt(i, j - 1, l);
        mz[i, j] = 0;
    }
    if (i < l && mz[i + 1, j] == 0)
    {
        ok = false;
        mz[i, j] = 3;//3 aka go down 
        bt(i + 1, j, l);
        mz[i, j] = 0;
    }
    if (j < l && mz[i, j + 1] == 0)
    {
        ok = false;
        mz[i, j] = 4;//4 aka go right 
        bt(i, j + 1, l);
        mz[i, j] = 0;
    }
    Console.WriteLine("out" + '\n' + Program.print(mz, l) + 'i' + i + 'j' + j + '\n');  //debug message for exiting
    if (ok) //this is for printing the solution when it is found
    {
        mz[i, j] = 8;// 8 aka the end
        foreach (int x in mz)
        {
            if (x == 0) { ok = false; break; }
        }
        if (ok)
            Console.WriteLine("result" + '\n' + Program.print(mz, l));
    }
}

mz[i, j] = 0; .

, mz . - , , .

+1

Source: https://habr.com/ru/post/1681615/


All Articles