waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認認真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          來源:http://blog.chinaunix.net/u1/50399/showart_410126.html

          public class DFSTest
          { 
             public static void main(String[] args)
             {
                 int[][] graph={
                  {0,1,1,0,0,0,0,0},
                  {1,0,0,1,1,0,0,0},
                  {1,0,0,0,0,1,1,0},
                  {0,1,0,0,0,0,0,1},
                  {0,1,0,0,0,0,0,1},
                  {0,0,1,0,0,0,1,0},
                  {0,0,1,0,0,1,0,0},
                  {0,0,0,1,1,0,0,0},
                 };
                 int[] list;
                 DFS dfs=new DFS();
                 dfs.input(graph, 0);
                 list=dfs.getList();
                 for(int i=0; i<graph.length; i++){
                     System.out.print(list[i]+" ");
                 }
             }
          }

          class DFS
          {
              int[][] graph;
              int[] list;
              
              int[] visited;
              int j;
              
              void input(int[][] graph, int v)
              {
                  this.graph=graph;
                  visited=new int[graph.length];
                  list=new int[graph.length];
                  for(int i: visited) i=0;
                  j=0;
                  calculate(v);
              }
              
              void calculate(int v)
              {
                  visited[v]=1;
                  list[j++]=v;
                  for(int k=0; k<graph.length; k++){
                      if(graph[v][k]==&& visited[k]==0){
                          calculate(k);
                      }
                  }
              }
              
              int[] getList()
              {
                  return list;
              }
          }


          posted on 2009-04-15 22:24 weesun一米陽光 閱讀(307) 評論(0)  編輯  收藏 所屬分類: JAVA源碼總結備用
          主站蜘蛛池模板: 罗田县| 卢氏县| 东辽县| 富裕县| 西吉县| 嘉义市| 沈丘县| 定南县| 昆山市| 和田县| 尉氏县| 体育| 阿巴嘎旗| 文昌市| 南部县| 隆安县| 兴义市| 姚安县| 尼勒克县| 泾阳县| 平舆县| 罗源县| 辽阳县| 从江县| 龙泉市| 林州市| 蓝田县| 安达市| 长子县| 沂南县| 虎林市| 改则县| 怀宁县| 西安市| 和田市| 永靖县| 印江| 应用必备| 泰州市| 喀喇沁旗| 永川市|