Java—有多少个数字组合

Java—有多少个数字组合,java,numbers,combinations,Java,Numbers,Combinations,任何人都可以告诉我如何在Java中解决以下问题。如果有效数字是0-9之间的任何数字,长度为10位,不包括或*,那么一盘棋在穿过电话键盘时可以跟踪多少个可能的有效数字。这里说我有一个国王,它只能像在真正的游戏中一样向任何方向移动,但一次只能移动一个细胞 所以键盘看起来像这样: 1 2 3 4 5 6 7 8 9 * 0 # // Returns a list of all the destinations g

任何人都可以告诉我如何在Java中解决以下问题。如果有效数字是0-9之间的任何数字,长度为10位,不包括或*,那么一盘棋在穿过电话键盘时可以跟踪多少个可能的有效数字。这里说我有一个国王,它只能像在真正的游戏中一样向任何方向移动,但一次只能移动一个细胞

所以键盘看起来像这样:

         1  2  3
         4  5  6
         7  8  9
         *  0  #
// Returns a list of all the destinations given a current location
// and the number of moves allowed (10 for King)
list<int> get_destinations(int cur_location, int num_moves) {
  // Base case. If no more moves are allowed, return current location as list.
  if(num_moves == 0) {
    return as_list(cur_location);
  } else {
    // List of all destinations possible from here
    list<int> destinations;
    // Get all the possible moves that are 1 step away.
    // For example: from 1, we would return 2 and 4
    list<int> possible_moves = get_possible_moves(cur_location);

    // For each of the moves generated from the last step,
    // recursively call get_destinations() with (num_moves - 1)
    for(int n:possible_moves) {
      destinations += get_destinations(n, num_moves - 1);
    }

    return destinations;
  }
}
get_unique_nums(get_destinations); // Returns the set in the form you need it
因此,工件每次移动10次,由此创建的每个唯一数字都是有效数字。一件作品从最初的起始位置开始它的旅程

更新: 一个工件可以移动或停留在一个地方,移动或停留都将被视为移动,并且只要在各自移动权限内允许,就可以重新访问单元。因此,例如,如果国王从位置1移动,则创建有效数字的三个有效10移动路径可能是1236547890或1111111111或12121212

以下是用于测试的小型四单元方形垫的代码,其中只有4个单元:

public class King
{
private static final Integer[] ALLOWED_FROM_1 = {2, 3, 4};
private static final Integer[] ALLOWED_FROM_2 = {1, 3, 4};
private static final Integer[] ALLOWED_FROM_3 = {1, 2, 4};
private static final Integer[] ALLOWED_FROM_4 = {1, 2, 3};
List<Integer> visited;

public King()
{
    this.visited = new ArrayList<Integer>();

}

public List<Integer> get_destinations(int currentPos, int noOfMoves)
{
    if (noOfMoves == 0)
    {
        visited.add(currentPos);
        return visited;

    }
    else
    {

        List<Integer> possibleMoves = getPossibleMoves(currentPos);

        for (int i = 0; i < possibleMoves.size(); i++)
        {
            visited.add(possibleMoves.get(i));
            get_destinations(possibleMoves.get(i), noOfMoves - 1);

        }

        return visited;
    }



}


private List<Integer> getPossibleMoves(int currentPos)
{

    List<Integer> possibleMoves = new ArrayList<Integer>();

    switch (currentPos)
    {
        case 1 : possibleMoves.addAll(Arrays.asList(ALLOWED_FROM_1));
            break;

        case 2: possibleMoves.addAll(Arrays.asList(ALLOWED_FROM_2));
            break;

        case 3 : possibleMoves.addAll(Arrays.asList(ALLOWED_FROM_3));
            break;

        case 4 : possibleMoves.addAll(Arrays.asList(ALLOWED_FROM_4));
    }

    return possibleMoves;

}
}
上面的代码只生成缺少许多不同排列的部分答案。主要问题是,我如何确切地确保它生成所有排列,以及在上述代码中,我在4次移动之后,应该存储并稍后检索到4位数字的确切时刻。另外,我如何避免重访相同的序列,例如1234 1234,因此基本上对其进行优化,使其不会产生相同的路径序列/有效数字


非常感谢大家的帮助。

下面是一些可能有用的伪代码。您可以使用以下方法递归解决问题:

         1  2  3
         4  5  6
         7  8  9
         *  0  #
// Returns a list of all the destinations given a current location
// and the number of moves allowed (10 for King)
list<int> get_destinations(int cur_location, int num_moves) {
  // Base case. If no more moves are allowed, return current location as list.
  if(num_moves == 0) {
    return as_list(cur_location);
  } else {
    // List of all destinations possible from here
    list<int> destinations;
    // Get all the possible moves that are 1 step away.
    // For example: from 1, we would return 2 and 4
    list<int> possible_moves = get_possible_moves(cur_location);

    // For each of the moves generated from the last step,
    // recursively call get_destinations() with (num_moves - 1)
    for(int n:possible_moves) {
      destinations += get_destinations(n, num_moves - 1);
    }

    return destinations;
  }
}
get_unique_nums(get_destinations); // Returns the set in the form you need it

祝你的家庭作业好运

似乎是一个非常琐碎的学术递归问题。只要允许递归,语言就不重要

你需要:

设立 A.在您的案例中,值为3x4的Fn,m数组 B以及工件的初始位置。 C要允许任意片段,您可能需要定义delegatefunction指针,即定义允许移动的匿名类函数

创建一个可递归调用的函数,用于标识下一个合格的位置,并将其作为输入: A.当前工件位置i,j, B第一次迭代中的递归深度为1

从深度为10的递归函数返回

如果您还需要所有序列,那么让这个递归函数返回非void类型,我认为字符串是最好的

如果您想使编码更简单,则不需要验证*的存在,在允许的单元格集中的每个步骤上,只需验证最后10个符号的长字符串是否可以作为无符号长字符串进行解析即可。尽管这会使递归太长。
对于一个国王来说,这种重复即将来临。 2,1,2,1,3,4,3,1,2,3,4,2,3,4,3,1

可以使用getPossibleMove函数以特定顺序返回单元格。 这将有助于避免路径重复。仅以数字顺序表示,然后应返回

二,一,二,一,, 2, 1, 2, 3, 2, 1, 2, 5, 2, 1, 4, 1, 2, 1, 4, 5, 2,1,4,7

此外,如果不想让片段移动到同一单元格,只需在getPossibleMove函数中删除这些单元格即可


我没有得到要写的代码,所以只写了理论解释。

什么棋子?有吗?到目前为止,你尝试了什么?有固定的起始位置吗?听起来像是家庭作业,如果有,是否要添加相应的标签?如果不能重新访问牢房,最多可以移动9个国王或王后。好的。但是为什么要从深度=10的递归函数返回呢?。如果没有剩余有效的移动,请尽快返回,最多10步。谢谢Mike。。我尝试过类似的解决方案,但在递归方面迷失了方向。。我将使用模板的伪代码再试一次,并让您知道它是如何运行的..我已经尝试使用上面的伪代码,但得到了重复的路径。。。我在一个较小的键盘上试过,它只有四个键1,2,3,4和get_destinations1,4。。它返回120/4所以30个组合,但如果选中,我会看到重复的路径。。例如,这里是前四条路径,您可以看到第二条和第四条路径相同->>2、1、2、1、3、4、3、1、2、3、4、2、3、4、3、3、1。。如何防止这种情况发生getPossibleMoves是如何定义的?对不起,我已经包含了King类的其余部分above@foofighter:basecase应返回当前元素时,返回空列表。此外,您似乎缺少来自5+的案例。你现在放在那里的案例似乎是错误的。当然,从1可以得到的实际上是{2,4,5}。嗨,沙什瓦特。。谢谢你的评论,是的,但问题是什么特别的顺序。正如你从所附的代码中看到的,我把单元格按数字顺序排列,这是当我得到重复的结果时,就像你刚才描述的那样,我以前有过的。我真的希望昨晚玩它,因为我今天需要它,但是 经过最近几天的工作,我睡得很好。。所以,在我写作的时候,酿造一杯新鲜的茶,试着让它发挥作用。。