DFS 深度优先

BFS 广度优先

DFS或者BFS都是在联通区域内遍历节点的方法

用在二叉树上DFS有preOreder,inOrder,postOrder,BFS就是层次遍历。

在二叉树上的节点,只有两个选择,left 和right,即,对于每一个节点,in 有1个, out 有两个,有向图

在矩阵的节点上,有四个选择,up、down、left和right四种选择,即,即,对于每一个节点,in 有4个, out 有4个,有向图

在surrounded Regions 中可以使用DFS或者BFS来解决问题

下面以4x4 矩阵为例,说明DFS和BFS的工作过程

 #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std; void printArray(int *array, int size)
{
for(int i = ; i < size; i++)
cout << array[i]<< "\t" ;
cout << endl;
} void printVector(vector<char> array )
{
for(int i = ; i <array.size(); i++)
cout << array[i]<< "\t" ;
cout << endl;
} void printVector(vector<int> array )
{
for(int i = ; i <array.size(); i++)
cout << array[i]<< "\t" ;
cout << endl;
} class Solution {
queue<pair<int, int> > m_que;
public:
void dfs(vector<vector<int> > &board, int i, int j)
{
size_t row = board.size();
size_t col = board[].size(); if(i < || i > row- || j < || j > col-)
return;
if( board[i][j] == INT_MAX)
return;
cout << "(" << i <<"," << j << ") = " << board[i][j] << endl ;
board[i][j] = INT_MAX;//tag, in order to reverse back
dfs(board, i, j-);
dfs(board, i, j+);
dfs(board, i-, j);
dfs(board, i+, j);
} void fill(vector<vector<int> > &board, int i, int j){
size_t row = board.size();
size_t col = board[].size(); if(i< || i>=row || j< || j>=col || board[i][j]== INT_MAX)
return; pair<int, int> p ;
p.first = i;
p.second = j;
m_que.push(p); cout << "(" << i <<"," << j << ") = " << board[i][j] << endl ;
board[i][j]= INT_MAX; } void bfs(vector<vector<int> > &board, int i, int j)
{
fill(board, i, j); while(!m_que.empty())
{
pair<int, int> p = m_que.front() ;
m_que.pop(); i = p.first;
j = p.second; fill(board, i, j-);
fill(board, i, j+);
fill(board, i-, j);
fill(board, i+, j);
} } void dfs(vector<vector<int> > board)
{
if (board.empty()) return; dfs(board, ,); }
void bfs(vector<vector<int> > board)
{
if (board.empty()) return; bfs(board, ,); }
}; int main()
{
vector<vector<int> > board;
vector<int> a;
a.resize(, ); a[] = ;
a[] = ;
a[] = ;
a[] = ;
board.push_back(a); a[] = ;
a[] = ;
a[] = ;
a[] = ;
board.push_back(a); a[] = ;
a[] = ;
a[] = ;
a[] = ;
board.push_back(a); a[] = ;
a[] = ;
a[] = ;
a[] = ;
board.push_back(a); // board.clear();
Solution sl; for(int i = ; i < board.size(); i++)
printVector(board[i]); cout <<endl;
cout << "dfs" <<endl;
sl.dfs(board);
cout << "bfs" <<endl;
sl.bfs(board); for(int i = ; i < board.size(); i++)
printVector(board[i]); return ;
}

打印结果

[root@localhost surroundedRegions]# ./a.out

矩阵
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

dfs
(2,2) = 10
(2,1) = 9
(2,0) = 8
(1,0) = 4
(1,1) = 5
(1,2) = 6
(1,3) = 7
(0,3) = 3
(0,2) = 2
(0,1) = 1
(0,0) = 0
(2,3) = 11
(3,3) = 15
(3,2) = 14
(3,1) = 13
(3,0) = 12

bfs
(2,2) = 10
(2,1) = 9
(2,3) = 11
(1,2) = 6
(3,2) = 14
(2,0) = 8
(1,1) = 5
(3,1) = 13
(1,3) = 7
(3,3) = 15
(0,2) = 2
(1,0) = 4
(3,0) = 12
(0,1) = 1
(0,3) = 3
(0,0) = 0

DFS & BFS的更多相关文章

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

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

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  5. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  6. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  7. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  8. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  9. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

  10. ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)

    //POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...

随机推荐

  1. react native 底部按钮切换

    在react   native  中底部按钮的切换  主要的是运用的是<TabBarNavigator/>这个组件,具体的代码实现如下: render() { return ( <T ...

  2. IIS增加并发数

    网站出现这样的错误信息:Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@appConcurrentRequ ...

  3. css 兼容

    color:#0000FF\9; ;/*ie6,ie7,ie8*/ *color:#FFFF00;/*ie7*/ _color:#FF0000;/*ie6*/ body:nth-of-type(1) ...

  4. Matlab中reshape函数的使用

    reshape把指定的矩阵改变形状,但是元素个数不变, 例如,行向量: a = [1 2 3 4 5 6] 执行下面语句把它变成3行2列: b = reshape(a,3,2) 执行结果: b = 1 ...

  5. 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk, 添加  -Dmaven.multiModuleP ...

  6. 使用mobile.changePage()时出现的问题(转)

    使用mobile.changePage()页面跳转,当跳转到目标页面时,目标页面中的初始化js如$().ready()及其他引入的js都无法执 行,重新刷新页面后才会执行.想到changePage() ...

  7. 解决The current branch is not configured for pull No value for key branch.master.merge found in confi

    1.在本地工程目录找到config文件(我的是在E:\rocket\rocket\.git): 2.修改config文件内容为: [core] repositoryformatversion = fi ...

  8. [BZOJ 1055][HAOI2008]玩具取名(DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1055 分析: 比较难想的dp f[i][j][c]表示i..j能否压缩成字符c 那么怎 ...

  9. [bzoj 2005][NOI 2010]能量采集(容斥原理+递推)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 分析:首先易得ans=∑gcd(x,y)*2+1 然后我就布吉岛了…… 上网搜了下题解, ...

  10. SpringMVC实现Restful风格的WebService

    1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...