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. C语言 百炼成钢19

    /* 题目55: 有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";),要求写一个函数(接口),输出以下结果 1) 以逗号分割字符串, ...

  2. OSGEARTH三维地形开源项目

    第一章   OSGEarth介绍 第二章   OSGEarth编译环境配置 OSGEarth的编译环境配置随着版本的不同.运行平台的不同,也有很大的差异.本章主要以Windows XP SP3(x86 ...

  3. 简单通用JDBC辅助类封装

    哎,最近很好久没在博客园写点东西了,由于工作的原因,接触公司自己研发的底层orm框架,偶然发现该框架在调用jdbc操作的时候参考的是hibernate 里面的SimpleJdbcTemplate,这里 ...

  4. [CareerCup] 1.6 Rotate Image 翻转图像

    1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...

  5. [CareerCup] 12.1 Find Mistakes 找程序错误

    12.1 Find the mistake(s) in the following code: unsigned int i; ; i >= ; --i) printf("%d\n&q ...

  6. 信息安全系统设计基础_exp3

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础 班级:1353 姓名:吴子怡.郑伟 学号:20135313.20135322 指导教师: 娄嘉鹏 实验 ...

  7. 系统级I/O 第八周11.9~11.15

    第十章 系统级I/O cp1 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include ...

  8. 在coding上添加ssh-key

    第一步:检查有没有ssh-key 第二步:生成ssh-key 第三步:添加到coding上或者Github上. ls -al ~/.ssh ssh-keygen -t rsa -C "you ...

  9. 客户端禁用cookies后session是否还起效果

    设置session和cookies的代码(webform1.aspx) if (txtName.Text == "wlzcool") { Session["uid&quo ...

  10. 使用iScroll实现上拉或者下拉刷新

    上拉或者下拉刷新的需求在移动端是非常常见的需求,大部分情况下,实现这个效果都使用网上现有的解决方案,例如有人使用swiper这个插件, 也有人使用iScroll这个滚动插件.本文的示例是利用iscro ...