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一米陽光 閱讀(300) 評論(0)  編輯  收藏 所屬分類: JAVA源碼總結備用
          主站蜘蛛池模板: 福贡县| 洞口县| 轮台县| 定州市| 滦南县| 麟游县| 林口县| 云安县| 安宁市| 天台县| 徐汇区| 东港市| 霍山县| 海淀区| 菏泽市| 黄石市| 文成县| 塔城市| 新丰县| 福安市| 怀来县| 宁远县| 利津县| 大理市| 浦城县| 拉萨市| 汝南县| 南丰县| 上饶市| 洞口县| 新源县| 克山县| 武定县| 老河口市| 屏东市| 远安县| 郧西县| 保康县| 蒲江县| 灌云县| 黄大仙区|