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源碼總結備用
          主站蜘蛛池模板: 丹巴县| 申扎县| 咸丰县| 姚安县| 焦作市| 鸡泽县| 买车| 湖北省| 宁南县| 缙云县| 泽普县| 德钦县| 鹤山市| 宁陵县| 巴东县| 富阳市| 长丰县| 军事| 屏东市| 桂阳县| 桂东县| 武川县| 海伦市| 澳门| 新干县| 巴青县| 鲁山县| 长汀县| 闽清县| 旬邑县| 永济市| 富源县| 泾源县| 吉林省| 峡江县| 屏山县| 云林县| 都昌县| 三台县| 泸州市| 华池县|