首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打电话给3D青年Tableau?

打电话给3D青年Tableau?
EN

Stack Overflow用户
提问于 2015-09-27 04:59:36
回答 2查看 139关注 0票数 0

我正在创建一个最大的3D年轻图形,所以int将有最大的整数在图表中。所有行、列和窗格都按降序排序。我正试图为此创建maxify方法,但效果不太好。我尝试从这里修改youngify方法,到目前为止,我已经完成了以下工作:

代码语言:javascript
复制
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数组上调用时发生的情况的示例。

代码语言:javascript
复制
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

一点也不完整。有人能看到我错过了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-27 12:41:18

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

这就是您的程序输出所显示的:1垂直移动,与4和7交换,然后水平地与8和9交换。总之,youngify()方法正确地筛选了左上角元素,而不考虑数组的其余部分。让原始的youngify()方法尝试一下{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}数组,您将看到行为完全相同。

会反复筛选左上角元素来完成这项工作吗?不一定:如果最高的元素已经位于左上角的位置,那么就不会有进一步的排序了。

我建议对每个维度的多维数组“行”按“行”排序。我编写了以下代码:

代码语言:javascript
复制
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];
  }
}

希望这能帮上忙..。

干杯,

杰夫

票数 1
EN

Stack Overflow用户

发布于 2019-07-31 08:59:02

代码语言:javascript
复制
private void tableau_heapify(int r, int c) {
int[] left = this.getLeft(r, c);
int[] right = this.getRight(r, c);

int[] smallest = new int[]{r, c};

// System.out.println(r + ", " + c);
//check if the cell is less than both of its children
if (this.isCellValid(left) && this.matrix[left[0]][left[1]] < this.matrix[r][c]) {
    //save address of left cell
    smallest = left;
}

if (this.isCellValid(right) && this.matrix[right[0]][right[1]] < this.matrix[smallest[0]][smallest[1]]) {
    //save address of right cell
    smallest = right;
}

//Check if we found changed address in our smallest variables.
//If yes, swap them. And, recursively call this method on that cell address
if (this.isCellValid(smallest) && (smallest[0] != r || smallest[1] != c)) {
    //swap
    int t = this.matrix[r][c];
    this.matrix[r][c] = this.matrix[smallest[0]][smallest[1]];
    this.matrix[smallest[0]][smallest[1]] = t;

    // recursive call tableau_heapify
    this.tableau_heapify(smallest[0], smallest[1]);
}

}

我在这里找到了对这个问题最好的解释:年轻的Tableau

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32804746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档