hdu 1241 Oil Deposits (简单搜索)
题目:
InputThe 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.
OutputFor 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
题解:
逐个遍历,遇到‘@’就深搜它的周围,逐层深搜,直到把相连的所有@都变成*。
代码:
#include <iostream> using namespace std;
char a[][];
int n,m,sum=;
void dfs(int i,int j){
if(a[i][j]!='@'||i<||j<||i>=m||j>=n) return;
else{
a[i][j]='*';
dfs(i-, j-);
dfs(i-, j);
dfs(i-, j+);
dfs(i, j-);
dfs(i, j+);
dfs(i+, j-);
dfs(i+, j);
dfs(i+, j+);
}
} int main()
{
int i,j;
while(cin>>m>>n){
sum=;
if(n==||m==) break;
for(i=;i<m;i++)
for(j=;j<n;j++)
cin>>a[i][j];
for(i=;i<m;i++)
for(j=;j<n;j++){
if(a[i][j]=='@'){
dfs(i,j);
sum++;
}
}
cout<<sum<<endl;
}
return ;
}
hdu 1241 Oil Deposits (简单搜索)的更多相关文章
- HDU 1241 Oil Deposits DFS搜索题
题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU 1241 Oil Deposits (DFS)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
随机推荐
- 快速搭建一个vue开发环境
常规的搭建就是从npm到vue到webpack这样一个一个的来,这样一套下来说不上复杂但是也不简单.之前公司的vue后台项目 我还是写的轻车熟路的,因为前端同事已经把架子搭好了,环境配置的丝般顺滑,你 ...
- Power BI免费版(Free),专业版(Pro)以及增值版(Premium)授权功能对比, Server
Features of Power BI Report Server and the Power BI service Features Power BI Report Server Power BI ...
- 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
在Global.asax文件中的Application_Start()方法中加入以下代码 Database.SetInitializer<XXX>(null);
- nginx--default_server定义规则及配置
nginx 的 default_server 指令可以定义默认的 server 出处理一些没有成功匹配 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 de ...
- 「CF#554 div2」题解
A 水题一道. 题目的大致意思就是:给你两个集合,求集合间有多少数对和是奇数. 题解,开\(4\)个桶后,求一个\(min\)就可以了. #include <bits/stdc++.h> ...
- openflow packet_out和packet_in分析
任务目的 1. 掌握OpenFlow交换机发送Packet-in消息过程及其消息格式. 2. 掌握OpenFlow控制器发送Packet-out消息过程及其消息格式. 实验原理 Packet-In 使 ...
- yii2 gridview默认排序
Yii2 GridView 使用起来很方便,但是默认排序很是个问题,数据默认按 主键 正序排列 但是在使用过程中,大多数数据默认是 倒序才符合正常思维的. 第一次 的解决方法是在 直接为 Model添 ...
- MongoDB集群管理常用命令
1.以admin身份登录yqtrack_gather01库: mongo 127.0.0.1:27017/yqtrack_gather01 -u username -p password --auth ...
- eclipse 包 取消代码第一行package包名 自动补全时取消自动引入包名 修改名字 取消引用 自动导入publilc static void main(String[] args) {}
--项目 --包 包是为了管理类文件,同个包下不允许同名类文件,但不同包就可以,把类放在包里是规范 (https://zhidao.baidu.com/question/239471930532952 ...
- 什么是Annotation
Annotation 被称为注解,在Java开发中是相当常见的,通过注解,我们可以简化代码提高开发效率.例如Override Annotation,这个应该算是在开发过程中使用最多的注解了.注解(An ...