三维杨盘调用Maxify? [英] Calling Maxify on 3D Young Tableau?

查看:108
本文介绍了三维杨盘调用Maxify?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个3D的最大杨盘,所以INT [0] [0] [0]将在画面的最大整数。所有的行,列和窗格按降序排列。我试图创建这个maxify方法,但它不工作太清楚了。我试图修改从这里的youngify方法,这就是我有这么远:

I'm creating a max 3D young tableau, so int[0][0][0] will have the largest integer in the tableau. All rows, columns, and panes are sorted in descending order. I'm trying to create the maxify method for this, but it's not working too well. I tried modifying the youngify method from here and this is what I have so far:

private void maxify(int i, int j, int k) {
    int largex = 0, largey = 0, largez = 0, x = 0, y = 0, z = 0;
    while (true) {
        x = largex;
        y = largey;
        z = largez;
        if (x + 1 > i && x + 1 < tableau.length && tableau[x + 1][y][z] > tableau[x][y][z]) {
            largex = x + 1;
            largey = y;
            largez = z;
        }
        if (y + 1 > j && y + 1 < tableau[0].length
                && tableau[x][y + 1][z] > tableau[largex][largey][largez]) {
            largex = x;
            largey = y + 1;
            largez = z;
        }
        if (z + 1 > k && z + 1 < tableau[0][0].length
                && tableau[x][y][z + 1] > tableau[largex][largey][largez]) {
            largex = x;
            largey = y;
            largez = z + 1;
        }
        if (largex != x || largey != y || largez != z) {
            tableau[x][y][z] = tableau[x][y][z] ^ tableau[largex][largey][largez];
            tableau[largex][largey][largez] = tableau[x][y][z]
                    ^ tableau[largex][largey][largez];
            tableau[x][y][z] = tableau[x][y][z] ^ tableau[largex][largey][largez];
            maxify(largex, largey, largez);
        } else
            break;
    }
}

这里是被称为3x3x1阵列上时发生的情况的例子。

Here's an example of what happens when called on a 3x3x1 array.

Starting with this array:
123
456
789

After first loop:    
423
156
789

And so on....    
423
156
789

423
756
189

423
756
189

423
756
819

423
756
819

423
756
891

423
756
891

423
756
891

成品数组:

423

756

891

不是完全在所有排序。任何人都可以看到我从这个失踪?

is not completely sorted at all. Can anyone see what I'm missing from this?

推荐答案

原来 youngify()方法实际上并不排序整个二维数组!问题是,在检测到进行交换后,说的第2行,下一次迭代开始交换发生的位置(即,从第2行),而不管其他互换的,可能有必要在第一要素行,但在其他方面。换句话说,它首先垂直再水平进行筛选下来的第一个元素

The original youngify() method doesn't actually sort a whole two-dimensional array! The problem is, after having detected a swap to be done, say on the 2nd row, the next iteration starts where the swap occurred (that is, from the 2nd row), regardless of other swaps that could be necessary on elements of the first row, but on other dimensions. In other words, it sifts down the first element first vertically then horizontally.

这就是程序输出显示:1垂直行进时,被交换的4和7,然后水平,被交换的8和9。总之, youngify()方法正确地进行筛选下来左上角元素无论阵列的其余部分。给原 youngify()法一试 {{9,8,7},{6,5,4},{3, 2,1}} 数组,你将看到的行为是完全一样的。

That's what your program output shows: the 1 travels vertically, being swapped with the 4 and the 7, then horizontally, being swapped with the 8 and the 9. In summary, the youngify() method correctly sifts down the top left element regardless of the rest of the array. Give the original youngify() method a try with the {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}} array, you will see the behaviour is exactly the same.

会反复筛选下来左上角元素做这项工作?不一定。如果最高的元素已经是在左上角的位置,没有什么会是[更多]排列在所有

Would repeatedly sifting down the top left element do the job? Not necessarily: if the highest element is already at the top left position, nothing will be [further] sorted at all.

我的建议是行为每个维度排序的多维数组的行。我写了下面code:

What I suggest is sorting your multidimensional array "row" by "row" for each dimension. I wrote the following code:

private static void maxify(int[][][] tableau) {
  int ii = tableau.length;
  int jj = tableau[0].length;
  int kk = tableau[0][0].length;
  // Sort on k-rows
  for (int i = 0; i < ii; i++) {
    for (int j = 0; j < jj; j++) {
      // Since k-rows are already standard arrays, no temporary array needed here.
      reverseSort(tableau[i][j]);
    }
  }
  // Sort on j-rows
  for (int i = 0; i < ii; i++) {
    for (int k = 0; k < kk; k++) {
      int[] temp = new int[jj];
      for (int j = 0; j < jj; j++) {
        temp[j] = tableau[i][j][k];
      }
      reverseSort(temp);
      for (int j = 0; j < jj; j++) {
        tableau[i][j][k] = temp[j];
      }
    }
  }
  // Sort on i-rows
  for (int j = 0; j < jj; j++) {
    for (int k = 0; k < kk; k++) {
      int[] temp = new int[ii];
      for (int i = 0; i < ii; i++) {
        temp[i] = tableau[i][j][k];
      }
      reverseSort(temp);
      for (int i = 0; i < ii; i++) {
        tableau[i][j][k] = temp[i];
      }
    }
  }
}

private static void reverseSort(int[] a) {
  Arrays.sort(a);
  for (int i = 0; i < a.length / 2; i++) {
    a[i] ^= a[a.length - i - 1];
    a[a.length - i - 1] ^= a[i];
    a[i] ^= a[a.length - i - 1];
  }
}

我希望这将有助于...

I hope this will help...

干杯,

杰夫

这篇关于三维杨盘调用Maxify?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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