如何生成多维数组的随机地址?

时间:2022-03-30 21:35:39

I'm stuck please help!

我被困了请求帮助!

the size of the matrix is n and m.

矩阵的大小是n和m。

for eg-
n=4, m=3
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12

例如,n = 4,m = 3 1,2,3,4,5,6,7,8,9,10,11,12

How to generate random address?

如何生成随机地址?

for eg- (0,0), (1,1), (1,2), (2,2), (3,3);
-> The last address should represents the end element of the matrix.
-> the next address should be the address of the element which either right or down.

例如 - (0,0),(1,1),(1,2),(2,2),(3,3); - >最后一个地址应该代表矩阵的结束元素。 - >下一个地址应该是向右或向下的元素的地址。

    Random random = new Random();
    List<Integer> list = new ArrayList<>();
    int random_int_1 = 0, random_int_2 = 0;
    int i = 0;
    list.add(2);
    list.add(1);
    while (list.get(i) < list.get(i + 1)) {
        list.add(random_int_1 = random.nextInt(5));
        list.add(random_int_2 = random.nextInt(5));
        i++;
    }
    for (int j = 0; j < list.size() - 1; j++) {
        if (list.get(j) < list.get(j + 1)) System.out.print(list.get(j));
    }

And I'm improving my skills by solving such examples
Please tell me how should I solve such problems.
Give some to-do tasks

我通过解决这些例子来提高我的技能请告诉我如何解决这些问题。给一些待办事项

1 个解决方案

#1


1  

Oke, I'll try to help you along. As per your example you have a current point, from there the code should move the point randomly right and down. I'm still a bit fuzzy on the move either or move both question, but that's not that important, around step 2 and 3 you can define some logic to always do either, or do either or both. (Right now it's possible the point does not move! Think about this!)

哦,我会尽力帮助你。根据您的示例,您有一个当前点,从那里代码应该随机向右和向下移动点。我在移动中仍然有点模糊,或者移动这两个问题,但这并不重要,在第2步和第3步之间你可以定义一些逻辑来做任何一个,或做其中一个或两个。 (现在它可能不会移动!想想这个!)

What your algorithm should do is the following:

您的算法应该做的是以下内容:

  1. Determine if the currentpoint is on the edges of the matrix (location m=3, n=4 cannot move)
  2. 确定当前点是否在矩阵的边缘(位置m = 3,n = 4不能移动)

  3. If it's possible to move right, create a random 0 or 1. if 0 do nothing, if 1 move right.
  4. 如果可以向右移动,则创建一个随机0或1.如果0不执行任何操作,如果1向右移动。

  5. If it's possible to move down, create a random 0 or 1. if 0 do nothing, if 1 move down.
  6. 如果可以向下移动,则创建一个随机0或1.如果0不执行任何操作,如果1向下移动。

  7. Append the location to the list and update current position, go to step 1.
  8. 将位置附加到列表并更新当前位置,转到步骤1。

You can either achieve this with loops or with a recursive algorithm. Hopefully you can solve the problem with these todo's :)

您可以使用循环或递归算法实现此目的。希望你能解决这些待办事项的问题:)

#1


1  

Oke, I'll try to help you along. As per your example you have a current point, from there the code should move the point randomly right and down. I'm still a bit fuzzy on the move either or move both question, but that's not that important, around step 2 and 3 you can define some logic to always do either, or do either or both. (Right now it's possible the point does not move! Think about this!)

哦,我会尽力帮助你。根据您的示例,您有一个当前点,从那里代码应该随机向右和向下移动点。我在移动中仍然有点模糊,或者移动这两个问题,但这并不重要,在第2步和第3步之间你可以定义一些逻辑来做任何一个,或做其中一个或两个。 (现在它可能不会移动!想想这个!)

What your algorithm should do is the following:

您的算法应该做的是以下内容:

  1. Determine if the currentpoint is on the edges of the matrix (location m=3, n=4 cannot move)
  2. 确定当前点是否在矩阵的边缘(位置m = 3,n = 4不能移动)

  3. If it's possible to move right, create a random 0 or 1. if 0 do nothing, if 1 move right.
  4. 如果可以向右移动,则创建一个随机0或1.如果0不执行任何操作,如果1向右移动。

  5. If it's possible to move down, create a random 0 or 1. if 0 do nothing, if 1 move down.
  6. 如果可以向下移动,则创建一个随机0或1.如果0不执行任何操作,如果1向下移动。

  7. Append the location to the list and update current position, go to step 1.
  8. 将位置附加到列表并更新当前位置,转到步骤1。

You can either achieve this with loops or with a recursive algorithm. Hopefully you can solve the problem with these todo's :)

您可以使用循环或递归算法实现此目的。希望你能解决这些待办事项的问题:)