九度oj 题目1460:Oil Deposit
- 题目描述:
-
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的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- Windows计算机重置TCP / IP
传输控制协议 (TCP / IP)是Internet上使用的通信协议. 在Windows的早期版本中,TCP / IP是一个单独的可选组件,可以像其他任何协议一样删除或添加. 早期版本中,从Windo ...
- BZOJ 2539: [Ctsc2000]丘比特的烦恼
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 695 Solved: 260[Submit][Status][Discuss] Description ...
- 卓越管理的实践技巧(2)成功的委派任务 Setup for Successful Delegation
Setup for Successful Delegation 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的索引 ...
- Jquery二维码在线生成(不能生成图片文件)
附件地址:http://files.cnblogs.com/files/harxingxing/jQuery%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%9C%A8%E7%BA%BF% ...
- 爬虫2_python2
# -*- coding: UTF-8 -*- # 正则表达式模块 import re # 获取路径模块 import urllib #时间模块 import time def getHtml(url ...
- 在Linux下安装redis
http://www.cnblogs.com/xiaohongxin/p/6854095.html 追加: 通过配置文件启动最好先./redis.cli shutdown ,再到当前目录在./redi ...
- mongo 副本集+分片 配置
服务器规划如下: 副本集名称|服务器IP 192.168.56.111 192.168.56.112 192.168.56.113 shard1 3201 3201 3201 shard2 3202 ...
- Manjaro 添加国内源和安装搜狗输入法
Manjaro 系统虽然比 Ubuntu 用着稳定,但有些小地方没有 Ubuntu 人性化,比如默认安装完的系统貌似没有中国的,Ubuntu 估计是用的人多,所以安装完后会根据所在地给你配置更新的源. ...
- IOC容器和Bean的配置
IOC容器和Bean的配置 1 IOC和DI ①IOC(Inversion of Control):反转控制. 在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取 ...
- hihocoder1175 拓扑排序2
#1175 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论 ...