首页 > 代码库 > 算法联系:排列组合

算法联系:排列组合

1. Matrix.java

package net.wuhx.main;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Matrix {
	private String rowText = "";
	private int row = 0;
	private int col = 0;
	private Point[][] points = null;
	
	public Matrix() { }
	
	public Matrix(String text) {
		this.rowText = text;
		this.col = text.length();
	}
	
	public Matrix(String text, int row) {
		this.rowText = text;
		this.row = row;
		this.col = text.length();
		this.points = new Point[this.row][this.col];
		this.init();
	}
	
	public void init() {
		if(!"".equals(rowText) && row != 0 && col != 0 && points!= null) {
			for(int i=0; i<row; i++) {
				for(int j=0; j<col; j++) {
					points[i][j] = new Point(j,i,this.rowText.charAt(j)+"");
				}
			}
		}
	}
	
	public Point[] getChilNode(Point p) {
		if((p.getY()+1) < points.length) {
			return points[p.getY()+1];
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<String> paiLie() {
		List<String> list = new ArrayList<String>();
		if(this.points != null && this.points.length > 0) {
			for(Point p : this.points[0]) {
				sort(list, p, "");
			}
		}
		return list;
	}
	
	public List<String> paiLie(int m) {
		if(m > this.col) {
			System.err.println("Invalid parameter m:"+m);
		}
		this.row = m;
		this.points = new Point[this.row][this.col];
		this.init();
		return paiLie();
	}

	public void sort(List<String> list, Point p, String path) {
		if(!path.contains(p.getValue())) {
			path += p.getValue();
			Point[] ps = getChilNode(p);
			if(ps != null) {
				for(Point pp : ps) {
					sort(list, pp, path);
				}
			}else {
				list.add(path);
				System.out.println(path);
			}
		}
	}
	
	
	public Set<Set> zuHe() {
		Set<Set> set = new HashSet<Set>();
		if(this.points != null && this.points.length > 0) {
			for(Point p : this.points[0]) {
				sortSet(set, p, new HashSet<String>());
			}
		}
		return set;
	}
	
	public void sortSet(Set<Set> set, Point p, Set<String> path) {
		if(!path.contains(p.getValue())) {
			path.add(p.getValue());
			Point[] ps = getChilNode(p);
			if(ps != null) {
				for(Point pp : ps) {
					sortSet(set, pp, path);
				}
			}else {
				set.add(path);
				System.out.println(path);
			}
		}
	}
	
	public Point[][] getPoints() {
		return points;
	}
	public void setPoints(Point[][] points) {
		this.points = points;
	}
	public int getRow() {
		return row;
	}
	public void setRow(int row) {
		this.row = row;
	}
	public int getCol() {
		return col;
	}
	public void setCol(int col) {
		this.col = col;
	}

}



2.Point.java

package net.wuhx.main;

public class Point {
	
	private String value;
	private int x;
	private int y;
	
	public Point() { }
	
	public Point(int x, int y) {
		this.setX(x);
		this.setY(y);
	}
	public Point(int x, int y, String value) {
		this.setX(x);
		this.setY(y);
		this.setValue(value);
	}
	
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = http://www.mamicode.com/value;"{"+value+", ["+x+","+y+"]"+ "}";
	}
	
}



3 Main.java

package net.wuhx.main;

import java.util.List;

public class Main {

	public static void main(String[] args) {
//		Matrix m = new Matrix("ABCDE", 3);
//		List<String> list = m.paiLie();
		//排列
//		Matrix mm = new Matrix("123ABC");
//		List<String> list = mm.paiLie(20);
//		System.out.println(list.size());
		
		//组合
		Matrix m = new Matrix("ABCD", 3);
		m.zuHe();
	}

}



算法联系:排列组合