[英]Print all the combinations of elements in matrix of size m * n
1 3 5
2 6 7
1 , 3 , 5
1 , 3 , 7
1 , 6 , 5
1 , 6 , 7
2 , 3 , 5
2 , 3 , 7
2 , 6 , 5
2 , 6 , 7
列數和行數可能會有所不同。 因此,解決方案必須是通用的。
import java.util.Scanner; class Combination { public static void main(String args[]) { int row, col, i, j; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns of matrix:\\n"); row = in.nextInt(); col = in.nextInt(); int first[][] = new int[row][col]; System.out.println("Enter the elements if matric m*n:\\n"); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { first[i][j] = in.nextInt(); } } System.out.println("Matrix:\\n"); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { System.out.print(first[i][j] + "\\t"); } System.out.println(); } // Final Logic from here... System.out.println("\\nOut Matrix:\\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { System.out.println(first[i][0] + "," + first[j][1] + "," + first[k][2]+"\\n"); } } } /* while (i < 2) { j = 0; while (j < 2) { k = 0; while (k < 2) { System.out.println(first[i][0] + "," + first[j][1] + "," + first[k][2]); k++; } j++; } i++; }*/ in.close(); } }
您可以按以下方式使用遞歸:
...
// Final Logic from here...
System.out.println("\nOut Matrix:\n");
int[] outputRow = new int[col];
print(0, row, col, first, outputRow);
}
private static void print(int j, int row, int col, int[][] first, int[] outputRow) {
for (int i = 0; i < row; i++) {
outputRow[j] = first[i][j];
// recursively continue to populate outputRow until we reach the last column (j == col -1)
if (j < col - 1) {
print(j + 1, row, col, first, outputRow);
}
// we have reached the last column (j == col -1) so now we could print current permutation
if (j == col - 1) {
for (int k = 0; k < col; k++) {
System.out.print(" " + outputRow[k]);
}
System.out.println();
}
}
}
在這里,我們從j==0
開始每個遞歸調用處理一列。
outputRow
存儲當前排列並遞歸更新。
當我們遞歸到達最后一列時,該打印當前排列了。
這是一種可能的方法
void printCombos(){
visit(0,-1,"");
}
void visit(int r,int c,String s){
if(c!=a[0].length-1)
for(int i=0;i<a.length;i++)
visit(i,c+1,s+" - "+a[i][c+1]);
else
System.out.println(s);
}
將矩陣視為需要深入研究的樹。 給定虛根*這些是邊(*,1)-(*,2)-(1,3)-(1,6)-(2,3)-(2,6)等
* --- 1 -- 3 -- 5
\ \/ \/
\ /\ /\
\ 2 -- 6 -- 7
5和7是樹葉。
首先創建另一個方法:
private static void increasePointerArray(int[] poinerArray, int row)
{
for (int i = poinerArray.length-1; i >= 0; i--) {
if(poinerArray[i] == row-1) {
continue;
}
else {
poinerArray[i] = poinerArray[i] +1;
for (int j = i+1; j < poinerArray.length; j++) {
poinerArray[j] = 0;
}
break;
}
}
}
現在,在最后的邏輯部分中,將以下代碼放入:
int[] poinerArray = new int[col];
int[] MaxArray = new int[col];
List<int[]> resultList = new ArrayList<int[]>();
Arrays.fill(poinerArray, 0);
Arrays.fill(MaxArray, row-1);
while(!Arrays.equals(poinerArray, MaxArray)) {
resultList.add(poinerArray.clone());
increasePointerArray(poinerArray, row);
}
resultList.add(poinerArray.clone());
System.out.println("Printing desired result : ");
for (int[] ks : resultList) {
StringBuffer sb = new StringBuffer();
for (j = 0; j < col; j++) {
sb.append(first[ks[j]][j]+"\t");
}
System.out.println(sb.toString());
sb = null;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Permutations{
public static String strings="";
public static ArrayList<String> out=new ArrayList<String>();
public static void gen(ArrayList<ArrayList<String>> x,int index){
for(int i=0;i<x.size();i++){
if(i>0){
String[] parts=strings.split(",");
strings="";
for(int k=0;k<parts.length;k++){
if(k==index)
break;
strings=strings+parts[k]+",";
}
}
if(index==x.get(0).size()-1){
strings=strings+(x.get(i).get(index));
out.add(strings);
}
else
strings=strings+(x.get(i).get(index))+",";
if(index+1<=x.get(0).size()-1)
gen(x,index+1);
}
}
public static void main(String[] args) throws IOException{
ArrayList<ArrayList<String>> x=new ArrayList<ArrayList<String>>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line;
while(true){
line=br.readLine();
if(line.contentEquals("")) break;
String[] parts=line.split(" ");
x.add(new ArrayList<String>());
for(int i=0;i<parts.length;i++){
x.get(x.size()-1).add(parts[i]);
}
}
gen(x,0);
for(int i=0;i<out.size();i++){
System.out.println(out.get(i));
}
}
}
此代碼有效。 它非常通用,很容易理解。 我在2D陣列上進行了列明智的排列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.