在2d数组中移动元素并具有边界限制运动

问题描述 投票:0回答:1

我不知道以前是否已经回答过,但是如何在2d数组中移动元素?说这样的话,如果不可能,则不要移动它,因为在显示的图像中0可以交换并向上,向下,向左和向右移动,但下一步只能将三个选项(仅向上,向下和向左)限制在内] >

C#的新手,所以如果在线上有关于我的问题的有用示例,只需将其张贴在注释下即可

enter image description here

using System;

namespace moveElement
{
    public class move
    {
        static void Main(string[] args)
        {
            int[,] arr = { { 9, 1, 4 }, { 5, 0, 3 }, { 6, 8, 2 } };
            for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(arr[i, j] + " ");
                        }

                        Console.WriteLine();
                    }
        }
    }
}

我不知道以前是否已经回答过,但是如何在2d数组中移动元素?说这样的话,如果不可能,则不要移动它,因为在显示的图像中0可以交换并且...

c# arrays swap
1个回答
0
投票

您可以尝试与this post的答案类似的方法,将位置1的变量放入临时变量,然后将位置1设置为位置2的值,最后将位置2的值设置为在temp变量中:

© www.soinside.com 2019 - 2024. All rights reserved.