waysun一路陽(yáng)光

          不輕易服輸,不輕言放棄.--心是夢(mèng)的舞臺(tái),心有多大,舞臺(tái)有多大。踏踏實(shí)實(shí)做事,認(rèn)認(rèn)真真做人。

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評(píng)論 :: 0 Trackbacks
          來(lái)源: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一米陽(yáng)光 閱讀(300) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA源碼 、總結(jié)備用
          主站蜘蛛池模板: 上饶市| 临西县| 建德市| 巩义市| 陕西省| 勐海县| 惠安县| 邵阳市| 鄱阳县| 杂多县| 台山市| 海安县| 探索| 广东省| 霸州市| 开远市| 平顺县| 沙坪坝区| 禹州市| 阜城县| 微山县| 万山特区| 屏南县| 南岸区| 石棉县| 藁城市| 尚志市| 隆化县| 屏东县| 大城县| 普兰店市| 涪陵区| 甘谷县| 葵青区| 宜宾市| 阳春市| 泰安市| 崇礼县| 湛江市| 漳州市| 青海省|