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

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

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 久治县| 藁城市| 寻甸| 开平市| 永泰县| 从江县| 治县。| 湖南省| 全椒县| 上高县| 平山县| 赫章县| 巫溪县| 沧州市| 万山特区| 汕头市| 平陆县| 广安市| 赞皇县| 綦江县| 砀山县| 崇州市| 宽城| 桦甸市| 县级市| 灌南县| 濮阳市| 平乐县| 盈江县| 宝鸡市| 巴林右旗| 淄博市| 黄石市| 祁阳县| 会同县| 北安市| 铁岭县| 库伦旗| 乌拉特前旗| 阳东县| 东宁县|