Java-矩阵中不同组合之和的列表

Java-矩阵中不同组合之和的列表,java,guava,Java,Guava,假设我有一个矩阵 |1,2,3| |4,5,6| |7,8,9| 我想要一个不同组合的总和列表,比如12(1+4+7)、13(1+4+8)等等,直到我有了所有27个组合。除了使用数组和for循环之外,实现这一点的最佳方法是什么。我从Google Guava查看了表界面,但不确定这是否是最好的方法 不同的组合-从上述矩阵中,我生成不同的组合,如(1,4,7),(1,4,8),(1,4,9),(1,5,7),(1,5,9),(1,6,7),(1,6,8),(1,6,9),(2,4,7),(2,4,

假设我有一个矩阵

|1,2,3|
|4,5,6|
|7,8,9|
我想要一个不同组合的总和列表,比如12(1+4+7)、13(1+4+8)等等,直到我有了所有27个组合。除了使用数组和for循环之外,实现这一点的最佳方法是什么。我从Google Guava查看了表界面,但不确定这是否是最好的方法


不同的组合-从上述矩阵中,我生成不同的组合,如(1,4,7),(1,4,8),(1,4,9),(1,5,7),(1,5,9),(1,6,7),(1,6,8),(1,6,9),(2,4,7),(2,4,8)等等,直到我得到所有27个组合,然后对每个组合中的值求和。

特别是看看这个项目,包。

您可以使用递归。我还没有测试下面的代码,但它应该可以做到这一点

//make this an instance field that the function sum() can access
ArrayList<Integer> matrixSums = new ArrayList<Integer>();

//call this line to put all the sums into matrixSums
getSums(matrix);

//helper function for the recursion (gets it started)
public void getSums(int[][] array)
{
    boolean[][] visited = new boolean[array.length][array[0].length];
    ArrayList<Integer> answer = new ArrayList<Integer>();

    for (int i = 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            visited[i][j] = true;
            sum(array, visited, 0, 0);
            visited[i][j] = false;
        }
    }

    return answer;
}

//does the actual recursion/math
public void sum(int[][] array, boolean[][] visited, int numVisited, int currentSum)
{
    //base case
    if (numVisited == 3)
    {
        matrixSums.add(currentSum);
        return;
    }

    //calculate sums
    for (int i = 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if (!visited[i][j])
            {
                visited[i][j] = true;
                sum(array, visited, numVisited + 1, currentSum + array[i][j]);
                visited[i][j] = false;
            }
        }
    }
}
//使其成为函数sum()可以访问的实例字段
ArrayList matrixSums=新的ArrayList();
//调用这行将所有的和放入矩阵
获取和(矩阵);
//递归的helper函数(启动它)
public void getSums(int[][]数组)
{
boolean[][]已访问=新的boolean[array.length][array[0.length];
ArrayList answer=新的ArrayList();
for(int i=0;i
您可以使用如下简单的递归函数:

 private static void findCombinations(int[][] matrix, int row, Integer[] currentArray) {
    if(row == matrix.length){
        LinkedList<Integer> list = new LinkedList<>(Arrays.asList(currentArray));
        result.add(list);
        return;
    }
    int cols = matrix[0].length;
    for(int i=0;i< cols;i++){
        currentArray[row]=matrix[row][i];
        findCombinations(matrix,row+1,currentArray);
    }
}

最后,您将在
result
列表中拥有所有27个组合,并且您可以轻松地对其进行迭代以获得总和。

如果您想要每个组合的总和,可以使用下面的方法。如果我能很好地理解你,它是经过测试并有效的:

基于二维表格的矩阵I(将打印)

Position类用于获取此矩阵表中数字的坐标

public class Position {

private int x;
private int y;

public Position(int x, int y){
    this.x=x;
    this.y=y;
}

public int getX()       {return x;}
public void setX(int x) {this.x = x;}
public int getY()       {return y;}
public void setY(int y) {this.y = y;}
public String toString(){
    return x+" x "+y;
}
}

矩阵有带矩阵的二维表,并在其上自行计算和

public class Matrix {

//table int[x][y]
private int[][] table;

/** calculate sum for any table of number position */
public int sum(Position... positions){
    int sum=0;
    for(Position temp: positions){
        int number=table[temp.getX()][temp.getY()];
        sum+=number;
        System.out.println(temp.getX()+" x "+temp.getY()+": "+number);
    }
    System.out.println("sum:\t"+sum);
    System.out.println("-------------------");
    return sum;
}

/** calculate sum of every combination on matrix and return as list */
public List<Integer> calulaceAllCombinationSums(){
    List<Integer> sums=new ArrayList<Integer>();
    int rows=table[0].length; //number of arguments in sum method
    Position[] arguments=new Position[rows]; //table of positions to calculate sum 
    int[] indexInRow=new int[rows]; // index of table represents row number, and value represents which number of row get to calculate sum
    for(int i=0;i<rows;i++){ //fill table with default values
        indexInRow[i]=0;
    }

    boolean finished=false; //specify if find all combinations
    int combinationNumber=0; //count combinations
    while(!finished){
        combinationNumber++;
        for(int i=0;i<rows;i++){
            arguments[i]=new Position(indexInRow[i], i);//prepare posistion of number as argument
        }
        sums.add(sum(arguments));//calculate sum and add to list of results

        finished=check(indexInRow); //checks if we are found every combination
        recalculateIndexRow(indexInRow);//change combination to next
    }
    System.out.println("all combinations: "+combinationNumber);

    return sums;
}

/** check if all combination are used */
private boolean check(int[] rows){
    boolean result=true;
    for(int i=0;i<rows.length;i++){
        if(rows[i]<(table.length-1)){
            result=false;
        }
    }
    return result;
}

/** recalculation of inedexes bases on incrementation each row from first until the end of row. 
 * Start with first row, first position. Increments position until the end of row, then increased by 1 second row, and set first row on first position.
 * And works like that over and over again until last position of last row  */
private void recalculateIndexRow(int[] rows){
    for(int i=0;i<rows.length;i++){
        if(rows[i]<(table.length-1)){
            rows[i]=rows[i]+1;
            break;
        }else if(rows[i]==table.length-1){
            rows[i]=0;
        }
    }
}

//getters and setters below
public int[][] getTable() {return table;} //getter
public void setTable(int[][] table) {this.table = table;}//setter
公共类矩阵{
//表int[x][y]
私有int[][]表;
/**计算任意数字位置表的总和*/
公共整数和(位置…位置){
整数和=0;
用于(职位温度:职位){
int number=表[temp.getX()][temp.getY()];
总和+=数字;
System.out.println(temp.getX()+“x”+temp.getY()+”:“+编号);
}
System.out.println(“总和:\t”+sum);
System.out.println(“------------------------”;
回报金额;
}
/**计算矩阵上每个组合的总和,并作为列表返回*/
公共列表calulaceAllCombinationSums(){
列表总和=新的ArrayList();
int rows=表[0]。长度;//sum方法中的参数数
Position[]arguments=新位置[行];//要计算总和的位置表
int[]indexInRow=new int[rows];//表的索引表示行数,值表示要计算和的行数
对于(int i=0;iWhy 12(1+4+7)?你能通过回答你的问题(请不要在评论中回答)来澄清你想做什么吗?我能理解12(1+4+7),你可能试图添加一行,但通过添加(1+4+8)你想做什么。
public class Position {

private int x;
private int y;

public Position(int x, int y){
    this.x=x;
    this.y=y;
}

public int getX()       {return x;}
public void setX(int x) {this.x = x;}
public int getY()       {return y;}
public void setY(int y) {this.y = y;}
public String toString(){
    return x+" x "+y;
}
public class Matrix {

//table int[x][y]
private int[][] table;

/** calculate sum for any table of number position */
public int sum(Position... positions){
    int sum=0;
    for(Position temp: positions){
        int number=table[temp.getX()][temp.getY()];
        sum+=number;
        System.out.println(temp.getX()+" x "+temp.getY()+": "+number);
    }
    System.out.println("sum:\t"+sum);
    System.out.println("-------------------");
    return sum;
}

/** calculate sum of every combination on matrix and return as list */
public List<Integer> calulaceAllCombinationSums(){
    List<Integer> sums=new ArrayList<Integer>();
    int rows=table[0].length; //number of arguments in sum method
    Position[] arguments=new Position[rows]; //table of positions to calculate sum 
    int[] indexInRow=new int[rows]; // index of table represents row number, and value represents which number of row get to calculate sum
    for(int i=0;i<rows;i++){ //fill table with default values
        indexInRow[i]=0;
    }

    boolean finished=false; //specify if find all combinations
    int combinationNumber=0; //count combinations
    while(!finished){
        combinationNumber++;
        for(int i=0;i<rows;i++){
            arguments[i]=new Position(indexInRow[i], i);//prepare posistion of number as argument
        }
        sums.add(sum(arguments));//calculate sum and add to list of results

        finished=check(indexInRow); //checks if we are found every combination
        recalculateIndexRow(indexInRow);//change combination to next
    }
    System.out.println("all combinations: "+combinationNumber);

    return sums;
}

/** check if all combination are used */
private boolean check(int[] rows){
    boolean result=true;
    for(int i=0;i<rows.length;i++){
        if(rows[i]<(table.length-1)){
            result=false;
        }
    }
    return result;
}

/** recalculation of inedexes bases on incrementation each row from first until the end of row. 
 * Start with first row, first position. Increments position until the end of row, then increased by 1 second row, and set first row on first position.
 * And works like that over and over again until last position of last row  */
private void recalculateIndexRow(int[] rows){
    for(int i=0;i<rows.length;i++){
        if(rows[i]<(table.length-1)){
            rows[i]=rows[i]+1;
            break;
        }else if(rows[i]==table.length-1){
            rows[i]=0;
        }
    }
}

//getters and setters below
public int[][] getTable() {return table;} //getter
public void setTable(int[][] table) {this.table = table;}//setter
public class Test {

public static void main(String...strings ){
    int[][] table=prepareMatrix();

    Matrix matrix=new Matrix();
    matrix.setTable(table);

    List<Integer> results=matrix.calulaceAllCombinationSums();
}

private static int[][] prepareMatrix(){
    int i=0;
    int[][] table =new int[3][3];
    System.out.println("*************************");
    System.out.println("\t TABLE");
    System.out.println("X | Y | value");
    System.out.println("------------");
    for(int y=0;y<table[0].length;y++){
        for(int x=0;x<table.length;x++){
            table[x][y]=i++;
            System.out.println(x+" | "+y+" | "+(i-1));
            System.out.println("------------");
        }
    }
    System.out.println("*************************\n");
    return table;
}