码迷,mamicode.com
首页 > 编程语言 > 详细

算法联系:排列组合

时间:2014-10-22 18:36:53      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:os   ar   java   for   on   amp   ad   new   size   

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 = value;
	}
	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;
	}
	
	@Override
	public String toString() {
		return "{"+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();
	}

}



算法联系:排列组合

标签:os   ar   java   for   on   amp   ad   new   size   

原文地址:http://my.oschina.net/u/1015065/blog/336319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!