Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从一个点开始用递增的数字填充二维数组_C#_Arrays_Recursion - Fatal编程技术网

C# 如何从一个点开始用递增的数字填充二维数组

C# 如何从一个点开始用递增的数字填充二维数组,c#,arrays,recursion,C#,Arrays,Recursion,我想要实现的是: 8765678 764567 6543456 5 4 3 2 3 4 5 4 3 212 3 4 5 4 3 2 3 4 5 6543456 764567 8765678 1-从给定坐标(X;Y)开始的起点 for(var i = 0;i<array.GetLength(0);i++) { for(var j = 0;j<array.GetLength(1);j++) { array[i,j] = 1 + Math.Abs(targe

我想要实现的是:

8765678
764567
6543456
5 4 3 2 3 4 5
4 3 212 3 4
5 4 3 2 3 4 5
6543456
764567
8765678

1-从给定坐标(X;Y)开始的起点

for(var i = 0;i<array.GetLength(0);i++)
{
    for(var j = 0;j<array.GetLength(1);j++)
    {
        array[i,j] = 1 + Math.Abs(targetI - i) + Math.Abs(targetJ - j);
    }
}
for(var i=0;i在代码中使用Rec(x,y,0),其中mas是您的数组,maxx和maxy是数组的大小

static public void Rec(int x, int y, int counter)
    {
        if (mas[x, y] == 0)
        {
            counter++;
            mas[x, y] = counter;
            if (x - 1 >= 0)
            {
                Rec(x - 1, y, counter);
                if (y - 1 >= 0)
                    Rec(x - 1, y - 1, counter);
            }
            if (y - 1 >= 0)
            {
                Rec(x, y - 1, counter);
                if (x + 1 <= maxx)
                {
                    Rec(x + 1, y - 1, counter);
                }
            }
            if (x + 1 <= maxx)
            {
                Rec(x + 1, y, counter);
                if (y + 1 <= maxy)
                {
                    Rec(x + 1, y + 1, counter);
                }
            }
            if (y + 1 <= maxy)
            {
                Rec(x, y + 1, counter);
                if (x - 1 >= 0)
                {
                    Rec(x - 1, y + 1, counter);
                }
            }
        }
    }
static public void Rec(整数x、整数y、整数计数器)
{
如果(mas[x,y]==0)
{
计数器++;
mas[x,y]=计数器;
如果(x-1>=0)
{
Rec(x-1,y,计数器);
如果(y-1>=0)
Rec(x-1,y-1,计数器);
}
如果(y-1>=0)
{
Rec(x,y-1,计数器);
如果(x+1无需递归:)

基本上,您希望数组中填充单元格坐标和目标坐标之间的值,即
abs(x-tx)+abs(y-ty)+1

这里有一个JavaScript解决方案,它只打印出这样一个数组;读者(我一直想这么说!)可以把它翻译成C#,并将它分配给自己喜欢的数组

function fillArray(w, h, targetX, targetY) {
    for(var y = 0; y < h; y++) {
        var t = [];
        for(var x = 0; x < w; x++) {
            t.push(Math.abs(x - targetX) + Math.abs(y - targetY) + 1);
        }
        console.log(t);
    }
}

fillArray(7, 9, 3, 4);
[ 8, 7, 6, 5, 6, 7, 8 ]
[ 7, 6, 5, 4, 5, 6, 7 ]
[ 6, 5, 4, 3, 4, 5, 6 ]
[ 5, 4, 3, 2, 3, 4, 5 ]
[ 4, 3, 2, 1, 2, 3, 4 ]
[ 5, 4, 3, 2, 3, 4, 5 ]
[ 6, 5, 4, 3, 4, 5, 6 ]
[ 7, 6, 5, 4, 5, 6, 7 ]
[ 8, 7, 6, 5, 6, 7, 8 ]