网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either \*', representing the absence of oil, or
@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
\*
3 5
\*@\*@\*
\*\*@\*\*
\*@\*@\*
1 8
@@\*\*\*\*@\*
5 5
\*\*\*\*@
\*@@\*@
\*@\*\*@
@@@\*@
@@\*\*@
0 0
Sample Output
0
1
2
2
这个题BFS,DFS都可以求解,这里用BFS尝试做了一次
思路:
对于每个有油区域,找出所有与它同属一个oil pocket的有油区域,最后计算一共有多少个oil pocket。
?怎样去找出所有与它同属一个oil pocket
BFS:找到一个起点;
从这个点出发,枚举四周寻找有油区域;
顺序从找到的新的区域出发,循环上述过程,直到没有新的区域加入。
?怎样去标志同属一个oil pocket的有油区域
设置一个访问标志代表此区域有没有被包含过,这样的话调用BFS的次数就= oil pocket的数目。
当然DFS也是可以这样做的
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
const double eps=1e-8;
const double PI=acos(-1.0);
using namespace std;
struct Node{
int x,y;
}node;
![img](https://img-blog.csdnimg.cn/img_convert/c3a31fe343b4b7155103b4a7300b30d1.png)
![img](https://img-blog.csdnimg.cn/img_convert/4c5456273c6c06034fe924ff3d8efd5d.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**