首页 > 其他 > 详细

DFS

时间:2020-02-21 10:32:33      阅读:16      评论:0      收藏:0      [点我收藏+]

标签:一个   java   i++   ati   ole   system   

 

import java.util.*;

public class DFS {
// 存储结构
static int[][] es = {

{0,1,0,0,0,0,0,1,0},
{1,0,1,0,1,0,0,0,0},
{0,1,0,1,0,0,0,0,0},
{0,0,1,0,1,1,0,0,0},
{0,1,0,1,0,0,0,0,0},
{0,0,0,1,0,0,1,1,0},
{0,0,0,0,0,1,0,0,0},
{1,0,0,0,0,1,0,0,1},
{0,0,0,0,0,0,0,1,0}
};

static String res[] = { "1","2","3","4","5","6","7","8","9" };
static int o = res.length;
public static void main(String[] args) {
bfs();
}
static void bfs()
{

boolean b[] = new boolean[o];//某个是否值是否被访问
Stack<Integer> l = new Stack<Integer>();
b[0]=true;//找到第一个值 标记已被访问
l.push(0);
System.out.print(res[0]+" ");
aa:while (!l.isEmpty())
{
int k = l.peek();//取出最后一个放进去的值 不删除
for (int i = 0; i <o; i++)
{
if (!b[i]&&es[k][i]==1)
{
b[i]=true;
l.push(i);
System.out.print(res[i]+" ");
continue aa;//结束本次while循环
}
}
l.pop();//找不到没被访问的值 对应到下表 进行弹出最后一个放进去的值

}


}

}

 

DFS

标签:一个   java   i++   ati   ole   system   

原文:https://www.cnblogs.com/shiaguang/p/12340308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号