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
*

*@*@*
**@**
*@*@* @@****@* ****@
*@@*@
*@**@
@@@*@
@@**@
Sample Output



这题还是比较好理解的,有着重叠的子问题,调用递归即可,感觉和DP有点不同,

DP重叠子问题后会有状态的转移,这个只用递归即可。(个人理解,刚学,比较菜)

 #include<bits/stdc++.h>
using namespace std ; char a[][];
void DFS(int x, int y)
{
a[x][y]=;
int X[]={-,-,,,,,,-};
int Y[]={,,,,,-,-,-};
for(int k=; k<; k++){
int i=x+X[k];
int j=y+Y[k];
if(a[i][j]=='@'){
DFS(i,j);
}
}
}
int main ()
{
int n,m;
while(cin>>n>>m)
{
if(n==&&m==)
break; memset(a, , sizeof(a));
for(int i=; i<n; i++)
for(int j=; j<m; j++)
cin>>a[i][j]; int cnt=;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(a[i][j]=='@'){
cnt++;
DFS(i,j);
}
cout<<cnt<<endl;
}
return ;
}
 

HDU 1241 连通块问题(DFS入门题)的更多相关文章

  1. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  2. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  3. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  4. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  5. [HDU]1016 DFS入门题

    题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...

  6. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDU 2089 - 不要62 - [数位DP][入门题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  8. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  9. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. redis-3.2 集群

    目录 简介 集群简介 Redis 集群的数据分片 Redis 集群的主从复制模型 Redis 一致性保证 redis 集群间的通信 环境 安装Ruby 部署 安装Redis略 创建集群 集群节点信息 ...

  2. P1501 [国家集训队]Tree II(LCT)

    P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225 ...

  3. 细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!

    在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找到都是这个版本! [HttpPost] public Task<Hashtable> ImgUpl ...

  4. tcp编程 示例

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...

  5. Linux - TCP编程相关配置2

    100万并发连接服务器笔记之处理端口数量受限问题 第二个遇到的问题:端口数量受限 一般来说,单独对外提供请求的服务不用考虑端口数量问题,监听某一个端口即可.但是向提供代理服务器,就不得不考虑端口数量受 ...

  6. Codeforces 15E Triangles - 组合数学

    Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...

  7. Navicat Premium 12.0.18安装与激活(转)

    转载:https://www.jianshu.com/p/42a33b0dda9c 一.Navicat Premium 12下载 Navicat Premium 12是一套数据库开发管理工具,支持连接 ...

  8. Oracle使用——Linux系统下使用命令实现oracle数据库数据导入

    背景 在工作当中,数据库的备份及数据导入是必不可少的操作,在完全无界面的Linux操作系统中,我们应该怎样实现oracle数据库的导入呢 前提 服务器已配置ftp 模拟环境 一台linux应用服务器上 ...

  9. CentOS7下Docker中构建Jenkins容器

    背景 在CentOS搭建Docker完成后,我们需要在Docker中搭建Jenkins用来实现工程自动部署. 安装前提 jdk已安装,安装目录如:usr/java/jdk1.8.0_161 maven ...

  10. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...