2D数组-计算数字序列

时间:2018-10-02 15:22:34

标签: c# arrays .net matrix

我不知何故完全在软管上。从功能原理上可以理解,听起来不是特别困难吗?但是我真的无法提出可能的解决方案。

我有一个2D阵列。是8x8。每个字段可以包含1到7之间的数字。现在,我想“返回”每行垂直/水平的数字,这些数字彼此之间至少应连续3倍。因此,从理论上讲,pro / series可能存在两个数字序列。

现在是真正的问题:我如何才能返回宽度/高度连续至少3倍的数字!?对索引感到满意。我现在已经尝试了很多,既没有迭代也没有递归。但是我达到了预期的结果-还是我看不到树木茂密的森林...?

示例:

output

2 个答案:

答案 0 :(得分:0)

首先对一维数组进行此操作。

我们有一个由8个数字组成的数组。

public List<int> Get3InRow(int[] array)
    {
        List<int> found = new List<int>(); // the list of found numbers we return
        int counter = 1;  //counts the amount of identical numbers found.
        int number = array[0];  //the number we are counting.

        //now comes the for loop
        for (int i = 1; i < 8; i++)
        {
            if (array[i] == number) counter++; // if we find a match counter goes up
            else
            {  //else reset the counter and search for new number.
                number = array[i];
                counter = 1;
            }
            if (counter == 3) found.Add(array[i]); // if we find 3 matching numbers ad it to the found list
        }
        return found;
    }

现在要使用2d数组进行此操作。我们要做的就是添加另一个for循环。

public List<int> Get3InRow(int[,] array)
    {
        List<int> found = new List<int>(); // the list of found numbers we return
        int counter = 1;  //counts the amount of identical numbers found.
        int number = 9;  //the number we are counting. (we chose 9 because none of the numbers in the array have this number.)

        //now comes the for loop
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {                    
                if (array[i,j] == number) counter++; // if we find a match counter goes up
                else
                {  //else reset the counter and search for new number.
                    number = array[i,j];
                    counter = 1;
                }
                if (counter == 3) found.Add(array[i,j]); // if we find 3 matching numbers ad it to the found list
            }
        }

        //we repeat the for loops with i and j interchanged(in array[]) because now we want to check the other axis
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (array[j,i] == number) counter++; // if we find a match counter goes up
                else
                {  //else reset the counter and search for new number.
                    number = array[j,i];
                    counter = 1;
                }
                if (counter == 3) found.Add(array[j,i]); // if we find 3 matching numbers ad it to the found list
            }
        }
        return found;
    }

答案 1 :(得分:0)

问题解决了,我会找到解决方案的。

private static List<List<Point>> Test(int[][] Matrix, Point Position, List<List<Point>> Pos) {
  if (Position.Y < Matrix.Length && Position.X < Matrix[Position.Y].Length) {
    if (Similar(Matrix, new Point(Position.X, Position.Y), new Point(Position.X + 1, Position.Y))) {
      List<Point> List = Pos.LastOrDefault();
      if (List == null) List = new List<Point>();

      List.Add(new Point(Position.X, Position.Y));
      List.Add(new Point(Position.X + 1, Position.Y));

      Pos.Remove(Pos.LastOrDefault());
      Pos.Add(List.Distinct().ToList());
    } else Pos.Add(new List<Point>());

    return Test(Matrix, new Point(Position.X + 1, Position.Y), Pos);
  } else {
    if (Position.Y < Matrix.Length && Position.X == Matrix[Position.Y].Length)
      return Test(Matrix, new Point(0, Position.Y + 1), Pos);
  }

  return Pos.Where(Entity => Entity.Count > 0).ToList();
}