BFS (Bridth First Search) can be implemented by a queue.

Procedure is like this: (Q is Queue)

1, Put 1 in Q : ->1 (front)

2, Read the front of Q (which is 1) and put its children in Q: ->4->3->2

3, Repeat 2

....

Also, DFS (Depth First Search) can be implemented by a stack.

Procedure is like this: (S is Stack)

1, Put 1 in S: 1<- (bottom)

2, Read the top of S which is 1 and put its children into S: 2<-7<-8

3, Repeat 2

....

Think about it!

BFS and Queue的更多相关文章

  1. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  2. 32-1题:不分行从上到下打印二叉树/BFS/deque/queue

    题目 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 考点 1.广度优先遍历 2.binary tree 3.queue 4.deque 思路 按层打印:8.6.10.5.7.9.11 用ST ...

  3. Clone Graph leetcode java(DFS and BFS 基础)

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  4. AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)(转载)

    转载自:  http://m.blog.csdn.net/blog/Enjoying_Science/42008801 题目链接:http://acm.hust.edu.cn/vjudge/probl ...

  5. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  6. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  7. HDU1241(bfs)JAVA

    import java.util.Scanner;public class Main1241 { public static void main(String[] args) { Scanner ci ...

  8. HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash

    这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...

  9. POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

    题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...

随机推荐

  1. Spark Streaming的wordcount案例

    之前测试的一些spark案例都是采用离线处理,spark streaming的流处理一样可以运行经典的wordcount. 基本环境: spark-2.0.0 scala-2.11.0 IDEA-15 ...

  2. 架设自己的FTP服务器 Serv-U详细配置图文教程

    转自:http://www.jb51.net/article/31635.htm 所有不是很要求安全的情况下是可以用serv_U的,当然我们也可以通过一些设置,保证serv_u安全运行.这里就分享下s ...

  3. box2d 已知bug

    1.动态刚体与一个与静态刚体重叠的小的感应刚体在contactBegin时,有些时候无法侦测到

  4. C# 泛型 Func<object, string, bool> filter

    Func<object, string, bool>是泛型,你可以先把他看成一个普通类型,比如stringpublic class Func{ } // 自定义个普通类. Func fil ...

  5. 使用MyBatis3时 selectOne 方法返回null的问题记录

    不多废话,直接上干货. mapper配置: <resultMap type="User" id="usermap"> <result colu ...

  6. ZOJ 1655 FZU 1125 Transport Goods

    迪杰斯特拉最短路径. 1.every city must wait till all the goods arrive, and then transport the arriving goods t ...

  7. Ctrl+Alt+T恢复启动Ubuntu默认终端

    对于Ubuntu14.04,如果安装了terminator,那么快捷键Ctrl+Alt+T将不会启动自带的terminal,而是启动安装的terminator,如果想恢复回来,可以执行以下命令: su ...

  8. CentOS安装Ruby组件

    ruby安装#安装ruby组件yum install ruby ruby-irb ruby-devel rubygems rpm-build -y#安装Apache服务器yum install htt ...

  9. Linux shell 操作 postgresql,并设置crontab任务

    Linux shell 操作 postgresql:删除间隔日期的数据-删除指定日期的数据-vacuumdb 清理数据库 -清理日志 -定期执行脚本 *修改pg_hba.conf 设置本地连接无密码, ...

  10. 浙大pat 1029题解

    1029. Median (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incre ...