如何使用结构体来覆盖二维数组中的所有整数?

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

这是我的第一篇帖子,我现在正在学习一门编程课程,我目前的任务是创建一个整数(我们称它为蚂蚁),它将移动到一个二维数组中的所有整数(随机路径)。这是我目前的代码。


namespace Ant
{
    class Program
    {
        static void Main(string[] args)
        {
            int ant;
            int i = 0;
            int[,] numberGrid =
            {
                {1, 2},
                {3, 4},
                {5, 6},
                {7, 8},
                {9, 10},
                {10, 11},
                {11, 12},
                {13, 14},
                {15, 16},
                {17, 18},
                {19, 20},
            };
            do
            {

                Random rand = new Random();
                ant= rand.Next(numberGrid[10, 1]);
                Console.WriteLine(ant);
                i++;
            } while (i !=110);
            Console.WriteLine("It took {0} steps for the ant to cover all spaces!", i);
        }
    }
}

我有一个二维数组,并暂时将蚂蚁设置为随机路径 在它停止之前,它将继续移动110次。我应该把 struct 集成到这个程序中,这样蚂蚁就会一直走到它访问了 2d 数组的所有整数,而不是设定的次数,但是我完全不知道该怎么做。如果有人能帮我理解,那将是非常好的,谢谢你

c# arrays multidimensional-array struct
1个回答
0
投票

如果没有更多的细节来说明你是如何做到这一点的,听起来你需要把蚂蚁做成一个结构,并记录蚂蚁去过(或没有去过)的地方。这里有一个方法,虽然我相信在性能上有更好的方法。

static void Main(string[] args)
{
    var unvisitedSpaces = new List<Coordinates>
    {
        //I've used your numbers but should this be a full matrix i.e. [1,1], [1,2], [1,3] etc.?
        new Coordinates(1, 2),
        new Coordinates(3, 4),
        new Coordinates(5, 6),
        new Coordinates(7, 8),
        new Coordinates(9, 10),
        new Coordinates(11, 12),
        new Coordinates(13, 14),
        new Coordinates(15, 16),
        new Coordinates(17, 18),
        new Coordinates(19, 20) 
    };

    var ant = new Ant();
    int counter = 0;
    var r = new Random();
    var min = Math.Min(unvisitedSpaces.Min(x => x.X), unvisitedSpaces.Min(y => y.Y));
    var max = Math.Max(unvisitedSpaces.Max(x => x.X), unvisitedSpaces.Max(y => y.Y)) + 1;

    do
    {
        ant.X = r.Next(min, max);
        ant.Y = r.Next(min, max);
        counter++;

        //check if the ant hasn't visited this space by checking the unvisitedSpaces list.
        if (unvisitedSpaces.Any(c => c.X == ant.X && c.Y == ant.Y))
        {
            //if it hasn't visited (the list contains that set of coordinates) then remove it from the list as it's now visited it.
            var coord = unvisitedSpaces.FirstOrDefault(c => c.X == ant.X && c.Y == ant.Y);
            unvisitedSpaces.Remove(coord);
        }
    } while (unvisitedSpaces.Count() > 0);

    Console.WriteLine("It took {0} steps for the ant to cover all spaces!", counter);
    Console.ReadLine();
}

public struct Coordinates
{
    public int X { get; }
    public int Y { get; }

    public Coordinates(int x, int y)
    {
        X = x;
        Y = y;
    }
}

public struct Ant
{
    public int X { get; set; }
    public int Y { get; set; }
}

结果:enter image description here

UPDATE

增加了自动调整'随机'使用的最大和最小值的功能,通过从坐标矩阵中获取它们。因此,对矩阵的任何调整都应该包含在'蚂蚁'访问的空间中。

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