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
{
class btr
{
public short[,] mz = new short[,] { };
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');
if (i > 0 && mz[i - 1, j] == 0)
{
ok = false;
mz[i, j] = 1;
var x = new btr { };
x.mz = mz;
x.bt(i - 1, j, l);
}
if (j > 0 && mz[i, j - 1] == 0)
{
ok = false;
mz[i, j] = 2;
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;
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;
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');
if (ok)
{
mz[i, j] = 8;
foreach (int x in mz)
{
if (x == 0) { ok = false; break; }
}
if (ok)
Console.WriteLine("result" + '\n' + Program.print(mz, l));
}
}
}
class Program
{
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)
{
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 .