题目描述:

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.

输入:

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.

输出:

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.

样例输入:
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出:
0
1
2
2 此题用深度优先搜索来求解,找到每一个'@',将其能遍历到的'@'均改成'*'
能搜索几次就有几块.
 #include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; char map[][];
int n, m;
int dir[][] = { { , }, { , - }, { , }, { -, }, { , }, { , - }, { -, }, { -, - } }; void dfs(int x, int y) {
map[x][y] = '*';
for (int i = ; i < ; i++) {
int xt = x + dir[i][];
int yt = y + dir[i][];
if (xt >= && xt < m && yt >= && yt < n) {
if (map[xt][yt] == '@') {
dfs(xt, yt);
}
}
} }
int main(int argc, char const *argv[])
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (scanf("%d %d", &m, &n) != EOF && m != ) {
for (int i = ; i < m; i++) {
scanf("%s", map[i]);
}
int ans = ;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (map[i][j] == '@') {
ans++;
dfs(i, j);
}
}
}
printf("%d\n", ans);
}
return ; }

九度oj 题目1460:Oil Deposit的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. python os,sys模块的使用

    首先,os模块是用来与操作系统进行交互的模块,可以对操作系统上的一些东西进行操作 而sys是用来对解释器进行一些操作的 一.os os.getcwd() 获取当前工作目录,即当前python脚本工作的 ...

  2. Web端 session cookies Application viewstate

    URL传值/QueryString1.不占用服务器内存2.保密性差,传递值的长度有限 因为  上篇文章 保密性差,长度有限   传值有限只能传string类型的值 这篇文章学的知识是 session ...

  3. jsp另外五大内置对象之-out获取缓冲区大小

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  4. [windows]窗口文件夹中使用常见任务

    文件夹中使用常见任务,如截图所示增加红色框部分. 设置步骤: 我的电脑--〉右键--〉属性--〉高级选项--〉性能设置--〉自定义:勾选在文件夹中使用常见任务.

  5. sessionStorage 详解,特点,使用技巧,场景

    很早之前久知道sessionStorage ,也学习过,但没有实战使用过 .最近团队遇到一个问题<electronjs中打开新页面sessionStorage丢失>,让我有机会重新来认识一 ...

  6. 陆教授浅谈5G毫米波手机天线技术的发展现状和未来的应用场景

    近日,香港城大电子工程学系讲座教授陆贵文教授荣获英国皇家工程院院士荣衔,以表彰他在推动天线研究的卓越贡献.他研发的天线由L形探针馈电微带天线.磁电耦极天线,以至5G毫米波手机天线等技术,均在天线领域影 ...

  7. SG函数入门&&HDU 1848

    SG函数 sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3. ...

  8. python_82_标准库_random模块

    import random print(help(random.random)) #随机整数 print(random.randint(1,7))#生成一个[1, 7]的随机整数 print(rand ...

  9. 利用Vue.js实现登录/登出以及JWT认证

    JSON Web Token 入门教程:http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html 后端代码地址:https ...

  10. c++ 函数指针应用,定义一个方法,传入两个参数和一个函数指针,并返回结果

    #include <iostream> #include <string> using namespace std; double add(double x, double y ...