POJ1562(DFS)

ZOJ1709/POJ1562 油田问题/搜索/DFS GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。 阅读详情

POJ1562(DFS)


Oil Deposits

Description

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 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

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
题意

@代表有油田,*代表没有油田。相邻的两个@代表一个油田,输入一个二维数组找出这个数组里共有多少油田?

思路

1.这是一个典型的八皇后问题,需要从8个方向遍历搜索

2.DFS(深度优先搜索)、递归方法

3.找到一个@后,从8个方向遍历,并记录ans++

代码
import java.util.Scanner;

public class OilDeposits {
    int[][] map = new int[105][105];

    int[] xs = {-1, -1, -1, 0, 0, 1, 1, 1};
    int[] ys = {-1, 0, 1, -1, 1, -1, 0, 1};


    public void dfs(int x, int y) {
        this.map[x][y] = 0;
        for (int i = 0; i < 8; i++) {
            if (this.map[x + xs[i]][y + ys[i]] == 1) {
                dfs(x + xs[i], y + ys[i]);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            if (m == 0 && n == 0) {
                break;
            }
            OilDeposits od = new OilDeposits();
            int ans = 0;
            for (int i = 1; i <= n; i++) {
                String str = sc.next();
                for (int j = 1; j <= m; j++) {
                    if (str.charAt(j - 1) == '@') {
                        od.map[i][j] = 1;
                    }
                }
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (od.map[i][j] == 1) {
                        od.dfs(i, j);
                        ans = ans + 1;
                    }
                }
            }
            System.out.println(ans);
        }
        sc.close();
    }
}
ROS牛耕法全覆盖规划:从算法原理到清洁机器人实战解析 本文深入解析ROS牛耕法全覆盖规划算法在清洁机器人中的应用,从算法原理到实战优化技巧全面覆盖。详细介绍了地图预处理、区域分解和路径优化等关键步骤,并分享实际案例中的解决方案,帮助开发者提升机器人清扫效率和覆盖率。 阅读详情

相关推荐

用74LS160和74LS194搭个复古流水灯:从Multisim仿真到面包板实战(附完整电路图)

本文详细介绍了如何使用74LS160和74LS194芯片搭建复古流水灯电路,从Multisim仿真到面包板实战的全过程。通过解析核心器件特性、提供完整材料清单、仿真验证电路逻辑,以及实际搭建中的布局规划和调试技巧,帮助电子爱好者掌握这一经典数字电路设计项目。

weixin_28736355的博客 388

每日一题(二十九):Oil Deposite

题目描述: 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 divi...

老肥码码码 547

基于淘宝商城用户购物行为数据分析系统

本文使用HIve+superset技术对阿里云天池提供的淘宝用户购物数据进行处理分析,得到有价值的信息并对平台提出有建设性意义的意见。

m0_51388399的博客 2818

Java for HDU1241 Oil Deposits

HDU1241 Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a...

ecin520的博客 445

QT编程系列1--QT学习前需知

本周学习了一些QT编程方面的知识,学写了几个程序,这里把记录下来。一方面对前面学习的知识做一个总结,便于以后复习、再学习和应用;另一方面也和大家做个交流,有不足的地方请各位看官多指点。   首先是做一些学习之前的交代:一,学习QT需要哪些基础;二,学习QT有哪些用途,QT较其他的开发工具有哪些特点;三,学习QT的思路。

霜月孤鸟&RTEMS&TinyOS 1115

大白话系列:什么是微服务

用大白话和实际例子来通俗易懂地告诉你什么是微服务~

2401_87403086的博客 1242

日志13:bfs例题

InputOutputSample1 13 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 00122struct nmint x,y;nm a;nm b;int main()ans=0;i<=n;i++)j<=m;i<=n;i++)j<=m;j++)ans++;q.push(a)

Cziq7的博客 1176

小米汽车技术路线

从小米的电子电器架构来看,行业内EEA已经由域集中逐渐过度到域融合+区域控制的架构并向着中央计算+区域控制的方向发展。 而小米汽车智驾算法也从基于高精地图模块化架构,到去高精地图模块化架构,再到端到端架构,这也正是行业对于智驾算法演进的共识。

HITbingdu的博客 3785

Hdoj 1241 油井、DFS入门题:【题解】

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oi...

EP的别三日 663

告别C盘爆红!手把手教你将WSL2下的Ubuntu 22.04完整迁移到D盘(附清理指南)

本文详细指导如何将WSL2下的Ubuntu 22.04从C盘无损迁移到D盘,包括导出备份、注销原系统、导入新位置等关键步骤,并提供迁移后的优化配置、开发环境衔接及长期维护技巧,帮助用户彻底解决C盘空间不足问题。

weixin_28698059的博客 472

OpenClaw零基础教程:30分钟搭建你的第一个AI自动化助手

别再看那些满是代码、专业术语的教程了,那些都是给程序员看的。这篇教程,我把所有步骤拆到了极致,你跟着点鼠标就行,点错了都没关系,大不了重装一遍,5分钟的事。

2601_94871597的博客 96

油田问题(C)

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 th

唧唧歪歪的博客 976

【camera-radar】基于ROS的多传感器融合感知系统实现(雷达+相机)(2)

实现功能:代码下载地址:下载地址 1)基于深度学习的目标检测;2)基于雷达的距离估计和预测;3)多传感器感知结果融合模块。 相机感知模块解析 4. Run nodes: python3 fusion_node/main.py python3 gps_node/main.py python3 radar_node/main.py python3 2d_detection_node/main.py #(additional instructions in 2d_detection_node/README.m

踟蹰横渡口,彳亍上滩舟。 9950

POJ 1562 Oil Deposits【DFS】&&ZOJ 1709

题目链接:http://poj.org/problem?id=1562 题目大意:就是找有多少个油田,在垂直或者水平或者垂直方向上相邻都算作是属于同一个油田。解题思路:如果遇到@的话就进行八个方位搜索,然后搜索到@变成*避免重复搜索。 代码如下: View Code #include<stdio.h> #include<string.h> ch...

dianxuanzhi1019的博客 161

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ - 1562 Oil Deposits(DFS)

Problem Description 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. I

00后的博客 202

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

POJ 1562 Oil Deposits(dfs)

题目链接:POJ 1562 Oil Deposits dfs。 很简单的dfs,对于每个符合要求的位置搜索其周围的八个方向。 #include using namespace std; const int MAX_N = 100 + 10; char _map[MAX_N][MAX_N]; int n, m, cnt; void _search(int i, int j) {

fobdddf的专栏 817

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

POJ 1562 Oil Deposits(DFS:求八连通分量个数)

POJ 1562 Oil Deposits(DFS) http://poj.org/problem?id=1562 题意:输入是一个R行C列的字符网格,表示油田,问你这个网格中有多少个由油田构成的连通分量.其中上下左右对角线相邻的网格油都算一个连通分量. 分析:典型的DFS,我们只需要对于网格中的每个油格,做一次DFS即可.(且还需要该油格之前没有被走过). AC代码: #include

code 957

zoj 1709 poj 1562 Oil Deposits【邻接阵,dfs】

题目连接http://poj.org/problem?id=1562 大致题意:给出一个图找出一共多少块油田。油田用‘@’表示,与它相邻的8个区域如果还有油田‘@’则视为一块。 #include<stdio.h> char grid[101][101]; int n, m; int dir[8][2] = { {-1, -1}, {-1, 0}, {-...

weixin_30596023的博客 122

POJ - 1562 Oil Deposits (DFS/BFS找连通块的个数)

POJ - 1562 Oil Deposits (DFS找连通块的个数) The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a tim...

weixin_43179892的博客 229

POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket 。 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接变为*,这样可以避免重复搜索。 1 //POJ 1562 ZOJ 1709 2 3 #include <stdio.h> 4 #include <str...

weixin_30362083的博客 129

ACM-POJ 1562 DFS 深度优先搜索

这里说一下这道题的唯一陷阱就是在输入的时候测试数据 m n后面是有空格的,如果你是用%c读入的话会直接读空格。 #include <iostream> #include <cstdio> using namespace std; int m, n; char map[100][100]; int move[8][2] = { {-1, -1}, {-1,...

程序圆圆圆 2009

POJ 1562 Oil Deposits (dfs 水T)

题目链接:http://poj.org/problem?id=1562 题意:类似于帮派的问题,周围的(八个方向)@都是一个群体,给你一个矩阵,问你有几个群体 思路:这也算是一种方法吧,就是用DFS将八个方向搜到的@变成*,这样下次就不会搜到重复的了,一直到八个方向搜索完返回,就让num(群体数)++。   最近状态很差,这种简单的题目都不会做,难受o(╥﹏╥)o AC代码: #in...

仅余我与暮色平分此世界 212

POJ 1562 Oil Deposits (简单深搜)

链接:http://poj.org/problem?id=1562 思路:遍历每一个油田,当遍历到某一个油田时,查找出与它相连接的油田,标记或者换成‘*’,一边统计个数就行了; 代码如下: #include #include #include #include #define MAXN 105 #define RST(N)memset(N, 0,sizeof(N)) using nam

Epic丶玖遇 1340

POJ 2386

http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 5 6 using namespace std...

A1823085974的博客 90

poj1562 Oil Deposits

题目:http://poj.org/problem?id=1562 题意:就是求油田的块数,如果一个小油田位于另一个小油田的周围的小矩形上(周围八个小方格),那么认为这两个油田是连通的,求有多少个这样的连通块。 思路:DFS搜索,两重循环遍历矩阵,如果这个节点是油田,就从这一个节点出发进行DFS,然后把这个点变为*,再八个方向dfs。 代码: #include #include u

u014032715的专栏 451
上一篇: POJ1111(DFS)
下一篇: POJ1753(DFS)
shui2104
博客等级 码龄10年 22粉丝 104原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值