posts - 30,  comments - 28,  trackbacks - 0

          有6個(gè)英文字母,a,b,c ,d,e,f
          ?從中任意取N個(gè)(N<=6)來排列.
          已知 a 只能與 b 相連,?
          ???????? b不能和e,f相連
          ?????????c不能和a相連
          ???????? d不能和 a ,e相連
          ???????????f 不能和?a,b相連
          請(qǐng)打印出字母f或b 在末尾的組合順序,用Java實(shí)現(xiàn)

          我的解答:

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

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

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 乐至县| 清原| 红桥区| 南澳县| 天等县| 岐山县| 贵溪市| 凤阳县| 嘉黎县| 丹阳市| 九龙县| 绵阳市| 元江| 定州市| 涡阳县| 锦州市| 卫辉市| 明溪县| 安平县| 孟连| 广元市| 湖口县| 苏尼特右旗| 阿荣旗| 丽江市| 三门县| 中卫市| 上杭县| 麟游县| 东山县| 息烽县| 藁城市| 汝南县| 武山县| 博白县| 都江堰市| 靖州| 民和| 望江县| 微山县| 镇平县|