博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
L - Oil Deposits HDU - 1241(flood fill,dfs,基础搜索)
阅读量:5283 次
发布时间:2019-06-14

本文共 2631 字,大约阅读时间需要 8 分钟。

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

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

思路:flood fill算法,很简单,很重要,就是算连通块,找到一个油田就把他附近8个方向和自己全部标记一遍,对周围找的的油田也对其8个方向标记一遍(注意开vis数组,不然可能会T)。

#include 
#include
using namespace std;char maze[105][105];int vis[105][105];int dirx[] = {
1,-1,0,0,1,-1,1,-1};int diry[] = {
0,0,-1,1,-1,1,1,-1};int n,m;int ans = 0;int check(int x,int y){
if(x < 0 || y < 0 || x >= n || y >= m) return 1; if(maze[x][y] == '*') return 1; return 0;}void dfs(int x,int y){
if(check(x,y))//思考这个东西的顺序,我觉得不一定就要在这里,代码顺序并不是重点,但是代码本身的逻辑是不能变动的。 return; maze[x][y] = '*'; for(int i = 0;i < 8;i++) {
int tx = x + dirx[i]; int ty = y + diry[i]; dfs(tx,ty); }}int main(){
while(~scanf("%d%d",&n,&m)&&n&&m) {
ans = 0; for(int i = 0;i < n;i++) {
scanf("%s",maze[i]); } for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) {
if(maze[i][j] == '@') {
ans++; dfs(i,j); } } } printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/tomjobs/p/10612576.html

你可能感兴趣的文章
WPF文本框只允许输入数字[转]
查看>>
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
第一个项目--用bootstrap实现美工设计的首页
查看>>
使用XML传递数据
查看>>
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
java选择文件时提供图像缩略图[转]
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
string与stringbuilder的区别
查看>>