使用递归搜索二维数组 [英] Search in 2d array using recursion

查看:90
本文介绍了使用递归搜索二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现使用递归在2D数组中搜索的功能.
例如,我们有bool Array [10] [10].需要的是计算2D阵列中有多少束True相邻单元.
我的意思是,如果x [1] [1] == true && x [1] [2] == true && x [0] [3] == true且它们周围的单元格为假,则此计数器加一. br/> 这就像计算地板上有多少飞溅物




非常感谢.

I need to implement a function that searches in the 2D Array using recursion.
For example, we have bool Array[10][10]. What is needed is to count how many bunch of True neighboring cells in the 2D Array.
I mean if x[1][1]== true && x[1][2]==true && x[0][3]==true and the cells around them are false then this the counter adds one.
This is like counting how many splash are on the floor




Thanks a lot.

推荐答案

[回答OP澄清问题的答案]

下一步需要的是正确定义邻居.这是细胞自动机"和博弈论"相关部分的通常步骤.

请参阅有关 Cell Games 的介绍: http://en.wikipedia.org/wiki/Cell_games_ (cellular_automaton) [ ^ ].

例如,您可以将邻居定义为并排触摸,角(对角)或两者并存的单元格,或这些集合的某些子集,或其他.下一步将是块的定义:一次或多次卖出,两次或多次卖出或其他.

有了它,它或多或少可以有效地解决问题.我想说,这不是一个非常困难但又不是完全琐碎的问题,这是学习算法中的一个典型问题.

祝你好运,
—SA
[Answer in response to clarification of the problem by OP]

What you need as a next step is correct definition of a neighbor. This is a usual step in Cell Automatons and related parts of Game Theory.

See on introduction on Cell Games: http://en.wikipedia.org/wiki/Cell_games_(cellular_automaton)[^].

For example, you can define a neighbor as a cell touching side-by-side, by corners (diagonally) or both, or some subset of these sets, or something else. Next step would be the definition of the block: a single sell or more, two or more or something else.

When you have it, it can solve a problem, more or less effectively. Not a very hard yet not completely trivial problem I, would say, a typical one in learning algorithms.

Good luck,
—SA


您打算如何进行递归?它不是递归结构,因此迭代是一种更自然的方法.仅当子代与父代相同时才可以递归:例如,包含列表的列表,例如包含列表的列表.您的数据是(布尔数组)的数组.
How do you propose to do that with recursion? It''s not a recursive structure, so iterration is a more natural approach. You can only recurse when the children are the same as the parent: A List containing a List, containing a List, for example. Your data is an array of (array of bool)s.
foreach (bool[] ar in Array)
   {
   foreach (bool b in ar)
      {
      if (b)
         {
         count++;
         }
      }
   }


您将不得不使用嵌套的for循环.尝试下面的代码.
我尚未测试此代码.
You will have to use nested for loops. Try below code.
i have not tested this code.
int i, j, totalBoolCount = 0;
for(i=0;i<arr.Length;i++)
{
    for(j=0;j<arr[i].Length;j++)
    {
        if(arr[i][j] == true)
        {
            totalBoolCount++;
        }
    }
}
//print totalBoolCount here and see if it answers your question.


祝你好运

[edit]请不要尝试使用幻数,尤其是在与初学者交谈时.在外部循环中使用Array.Length和在内部循环中使用Array [i] .Length使得当数组大小由5变为5时,它们更可能不会出现问题! (哦,初始化变量!-OrignalGriff [/edit]


Good Luck

[edit]Please, try not to use Magic Numbers, particularly when talking to beginners. Using Array.Length in the outer loop and Array[i].Length in the inner makes it more likely they will not have problems when the array size changes to 5 by 8! (Oh, and initialize your variables! - OrignalGriff[/edit]


这篇关于使用递归搜索二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆