有6个英文字母,a,b,c ,d,e,f
从中任意取N个(N<=6)来排列.
已知 a 只能与 b 相连,
b不能和e,f相连
c不能和a相连
d不能和 a ,e相连
f 不能和 a,b相连
请打印出字母f或b 在末尾的组合顺序,用Java实现
我的解答:
import java.util.*;
/**
*
* @author ShenXiaoliang
*
*/
public class DemoGraph {
final static int VERTICE=6;
private ArrayList<String> patheSet=new ArrayList<String>();
private String[] ver={"a","b","c","d","e","f"};
private int[][] graph=new int[6][6];
private String path="";
private boolean[] isVisit=new boolean[VERTICE];
public DemoGraph() {
initiGraph();
for(int index=0;index<VERTICE;index++)
depthSearch(index);
show();
}
private void initiGraph(){
graph[0][1]=1;
graph[1][0]=1;
graph[1][2]=1;
graph[1][3]=1;
graph[2][1]=1;
graph[2][3]=1;
graph[2][4]=1;
graph[2][5]=1;
graph[3][1]=1;
graph[3][2]=1;
graph[3][5]=1;
graph[4][2]=1;
graph[4][5]=1;
graph[5][2]=1;
graph[5][3]=1;
graph[5][4]=1;
}
private void depthSearch(int start){
isVisit[start]=true;
path+=ver[start];
if(path.charAt(path.length()-1)=='f'||path.charAt(path.length()-1)=='b') patheSet.add(path);
for(int index=0;index<VERTICE;index++)
if(graph[start][index]==1&&isVisit[index]==false)
depthSearch(index);
else continue;
path=path.substring(0,path.length()-1);
isVisit[start]=false;
}
private void show(){
for(String pa:patheSet)
System.out.println(pa);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DemoGraph();
}
}
posted on 2006-10-12 00:43
murainwood 阅读(210)
评论(0) 编辑 收藏 所属分类:
Java读书笔记 、
随感