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. thinkphp 随笔

     'TMPL_CACHE_ON' => false,//禁止模板编译缓存 'HTML_CACHE_ON' => false,//禁止静态缓存 

  2. awk中截取IP字段

    由于文本的特殊性,IP字段可能并不是在特定的字段中. 借助awk的match()函数进行匹配截取 awk --re-interval '($0 ~ "xxx"){match($0, ...

  3. JS截取字符串中数字

    今天项目中需要在一个字符串中截取一个数字,然后数字参与运算.搜了一下,有好多好用的方式截取字符串. 1,使用parseInt() var str ="4500元"; var num ...

  4. canvas-简单快速实现知乎登录页背景效果

    前言 打开知乎的登录页,就可以看到其背景有一个动效,看起来好像蛮不错的样子: 这个效果使用canvas是不难实现的,接下来就一步一步地讲解并实现这个效果. 分析 在动工之前先分析这个效果到底是如何运动 ...

  5. shell脚本之 给PNG图片添加后缀@3x

    1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...

  6. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...

  7. Python3 tkinter基础 Frame event.x 输出鼠标左右键点击的位置

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. 嵌入式电路中的BUCK VS LDO【转】

    本文转载自:http://blog.chinaunix.net/uid-25906157-id-4125916.html 作为一名FAE,才知硬件知识的匮乏.慢慢积累一点儿硬件知识吧!BUCK和LDO ...

  9. Mac 配置教程-日常篇

    今年终于在推出 2018 款 MBP 时,看到升级了 CPU,我就果断下手「拔草」.本文记录使用 Mac 的一些配置,会长期更新. 为了控制文章的篇幅,我将 Mac 使用配置分成了两篇: Mac 配置 ...

  10. CentOS 安装 Gitlab

    源地址 https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/ # 清华源 https://mirrors.tuna.tsinghua.edu.cn/help/g ...