Java 如何选择矩阵二维中所有唯一的行和列

Java 如何选择矩阵二维中所有唯一的行和列,java,matrix,Java,Matrix,我实现了一个简单的源代码来查找矩阵中所有可能的路径。然后选择唯一的行和列。但是由于时间的复杂性,我们怎样才能直接得到唯一的路径呢? 这是我的源代码: import java.util.*; public class MyAllPath { public static ArrayList<String> allPaths; public static void printpaths(double[][] matrix) { String[]

我实现了一个简单的源代码来查找矩阵中所有可能的路径。然后选择唯一的行和列。但是由于时间的复杂性,我们怎样才能直接得到唯一的路径呢? 这是我的源代码:

    import java.util.*;
public class MyAllPath {
    public static ArrayList<String> allPaths;
    public static void printpaths(double[][] matrix) {  
        String[] path = new String[matrix.length];

    for (int i = 0; i < matrix[0].length; i++)
    {
        printpaths(matrix, path, 0, 0, i);
    }
}

private static void printpaths(double[][] matrix, String[] path, int index, int row, int column)
{
    path[index++] = Integer.toString(column)+"|"+Double.toString(matrix[row][column])+"\t";
    row++;
    if (row == matrix.length)
        {
            print(path);
        }
    else if (row < matrix.length) 
    {
        int boundary = matrix[0].length-1;
        for (int i = column - boundary; i <= column + boundary; i++)
        {
            if (i > -1 && i < matrix[0].length)
            {
                printpaths(matrix, path, index, row, i);
            }
        }
    }
}

private static void print(String[] path) 
{
    String myPath="";
    for (int i = 0; i < path.length; i++)
    {
        myPath+=path[i]+" ";
        System.out.print(path[i] + " ");
    }
    System.out.println();
    allPaths.add(myPath);
}


public static void main(String args[]) 
{
    allPaths =new ArrayList<String>();
    double[][] matrix = {{1, 2, 3},
                        {4, 5, 6}, 
                        {7, 8, 9}};
    printpaths(matrix);
    System.out.println("-------------------------------------------------------");
    for(int i=0;i<allPaths.size();i++)
    {
        String[] path= allPaths.get(i).split(" ");
        TreeSet<Integer> duplicateColumn = new TreeSet<Integer>();
        for(int j= 0;j<path.length;j++)
        {
            String[] word=path[j].split("\\|");

            for(int k=0;k<word.length;k++)
            {
                duplicateColumn.add(Integer.parseInt(word[0]));
            }
        }
        if(duplicateColumn.size()==path.length)
            System.out.println(allPaths.get(i));

    }
}
}

你粘贴了太多的代码:如果你只需要在提取重复项的优化方面得到帮助,那么只向我们展示部分+初始化和输出。无需显示所有打印功能。你能举例说明唯一的行和列吗?对于给定的init数据,什么应该计算,什么不应该计算。感谢您的回复,这是我的输出方式,您可以运行我的代码来获得输出0 | 1.0 1 | 5.0 2 | 9.0 0 0 | 1.0 2 | 6.0 1 | 8.0 1 | 2.0 0 2 12401 12402 12402 1240 12402 1240 1240 12402 1240 1240 1240 1240 12402 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240 1240=