【问题标题】:2D Array - counting number sequence二维数组 - 计数序列
【发布时间】:2018-10-02 15:22:33
【问题描述】:

不知何故,我完全在软管上。按功能原理来理解,听上去是不是特别难?但我真的想不出一个可能的解决方案。

我有一个二维数组。它是 8x8。每个字段可以包含 1-7 之间的数字。现在我想在垂直/水平方向上“返回”每行/行的数字,这些数字至少连续 3x 彼此落后或彼此相邻。因此理论上 pro / series 有可能存在两个数字序列。

现在真正的问题是:我如何才能返回宽度/高度至少连续 3 倍的数字!?对索引感到满意。我现在已经尝试了很多,既没有迭代也没有递归。但我得到了想要的结果——或者我只见树木不见森林......?

例子:

【问题讨论】:

  • “现在我想在垂直/水平方向上“返回”每行/行的数字,这些数字至少连续 3x 彼此落后或彼此相邻。” 嗯?您想在一行中找到相同数量的运行(或至少长度为 3)?您对结果究竟有何期望?
  • “我如何才能返回宽度/高度至少连续 3 倍的数字!?对索引感到满意。我现在尝试了很多,既没有迭代也没有递归。但我得到了想要的结果“ 在问题中包含您的最佳尝试(实际代码)。真的很难弄清楚你在找什么。你是从 int[,] 还是 int[][] 开始的?
  • “我现在尝试了很多,既没有迭代也没有递归” - 我们需要你的代码来帮助修复它。基本上,您只需使用 2 个循环即可。两个循环 ij 从 1 到 n-1 并检查是否 a[i][j-1] == a[i][j] == a[i][j+1]a[i-1][j] == a[i][j] == a[i+1][j],这样就完成了。
  • 你有没有尝试过这个一维数组?如果您可以计算出一维数组,那么您需要做的就是将其包装在每一行的另一个循环中。

标签: c# arrays .net matrix


【解决方案1】:

让我们先对一维数组执行此操作。

我们有一个包含 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;
    }

现在用二维数组来做这件事。我们所要做的就是添加另一个 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;
    }

【讨论】:

    【解决方案2】:

    问题解决了,我会找到解决办法的。

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多