BFS
永远的EMT
每天时刻保持超越自我的意识
展开
-
【PAT】1091. Acute Stroke
考查点:BFS思路:本质上就是立体的广搜,理论上深搜也可以但是会溢出,所以以后还是尽量用广搜,只要设置六个方向的三个数组,之后基本套路,用标记数组标记入队,每次出队时将设置的计数器自增,细节上注意小于t的是不需要计入的,最后在主函数上遍历时在合法节点上调用bfs,并更新ans提交情况:第一次因为开的三维数组有一维开小了,但答案没有报段错误,以后要注意。。收获:这类搜索题基本代码都差不多原创 2017-02-11 22:41:10 · 286 阅读 · 0 评论 -
【LeetCode】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3 But the follo...原创 2018-10-20 22:46:22 · 184 阅读 · 0 评论 -
【LeetCode】Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...原创 2018-10-20 18:43:00 · 154 阅读 · 0 评论 -
【LeetCode】126. Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time Eac...原创 2018-10-17 01:07:30 · 257 阅读 · 0 评论 -
【Leetcode】785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite.Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in ...原创 2018-08-28 14:58:19 · 318 阅读 · 0 评论 -
【PAT】1115. Counting Nodes in a BST
考查点:BST插入,DFS思路及提交情况:水题,计算最后两层的节点数,只需维护一个层数数组,记录该层的节点数,用DFS是最简洁的,这里开始遇到bug,首先新建节点要么new要么NULL,之后记得return#define LOCAL#include #include #include #include #include #include #include #include原创 2017-02-24 17:39:20 · 331 阅读 · 0 评论 -
【PAT】1076. Forwards on Weibo
考查点:BFS思路:第一次用DFS只有20分,有很多情况没有考虑,直接用BFS过了,BFS要记录每个点的层数,满足层数时更新个数#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define原创 2017-02-15 22:32:59 · 572 阅读 · 1 评论 -
【PAT】1021. Deepest Root
考查点:并查集求连通分量,DFS或BFS思路:本题数据很弱,第一次直接用DFS就可以过,主要犯了啃爹的错误,在写循环遍历容器时for(int i=0;i暴力过的代码:#include #include #include #include #include #include #include #include #include #include #include #原创 2017-02-15 00:24:42 · 254 阅读 · 0 评论 -
图的遍历BFS
邻接矩阵:int n,G[MAXN][MAXN];bool inq[MAXN];void BFS(int u){ queue q; q.push(u); inq[u]=true; while(!q.empty()) { int u=q.front(); q.pop(); FOR(v,0,n)原创 2017-02-14 11:25:49 · 521 阅读 · 0 评论 -
【PAT】1099. Build A Binary Search Tree
考查点:BST,BFS,中序遍历#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i < y; i++)#define rFO原创 2017-02-13 10:39:54 · 236 阅读 · 0 评论 -
【PAT】1004. Counting Leaves
考查点:BFS或DFS思路:本题为求各层叶节点个数,深搜和广搜都可以,深搜写起来更方便DFS版本:#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y)原创 2017-02-12 20:52:21 · 229 阅读 · 0 评论 -
【PAT】1079. Total Sales of Supply Chain
考查点:DFS或BFS思路:直接用DFS即可,代码简洁,没有难度#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i <原创 2017-02-12 18:08:12 · 298 阅读 · 0 评论 -
【PAT】1094. The Largest Generation
考查点:BFS或DFS,求树的最大节点层思路:BFS代码长点,DFS较短,BFS只需设置个变量last来记录每一层的最后一个节点;DFS只需两个参数,一个节点一个层数,每次调用都要更新当前层的节点数,可以用哈希数组保存层数的节点值提交情况:一开始输入时候%d写错成%k调了半天,之后因为找根节点时候没把0排除,第一次提交没考虑只有一个根节点的情况之后acBFS的代码:#define原创 2017-02-12 17:25:59 · 264 阅读 · 0 评论 -
POJ2386.Lake Counting
考查点:广搜或深搜思路:主要注意输出的处理,需要用getchar两次接收换行,剩下的基本上是标准的深搜或广搜#define LOCAL#include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int原创 2017-02-11 23:22:07 · 262 阅读 · 0 评论 -
【LeetCode】Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a ti...原创 2018-10-29 02:02:22 · 168 阅读 · 0 评论