首页 > 其他 > 详细

DFS

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

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!