posts - 30,  comments - 28,  trackbacks - 0

          有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 閱讀(220) 評論(0)  編輯  收藏 所屬分類: Java讀書筆記隨感
          <2006年10月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          如果真的給你一片天,你敢不敢要?

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太湖县| 绥江县| 清徐县| 睢宁县| 聂拉木县| 兴业县| 西乌| 宁津县| 建始县| 昌宁县| 华阴市| 香港 | 呼图壁县| 容城县| 顺义区| 巩留县| 德庆县| 咸宁市| 图们市| 双鸭山市| 库车县| 鹤壁市| 吴忠市| 涟水县| 上林县| 和顺县| 喜德县| 平陆县| 卢氏县| 大邑县| 保亭| 赤城县| 寿阳县| 长兴县| 兴化市| 依安县| 右玉县| 精河县| 凉山| 长沙县| 汤阴县|