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. 深入理解Word2Vec

    Word2Vec Tutorial - The Skip-Gram Model,Skip-Gram模型的实现原理:http://mccormickml.com/2016/04/19/word2vec- ...

  2. Golang实现冒泡排序法

    关于冒泡排序的原理请看本博客这篇文章冒泡排序法原理讲解及PHP代码示例 //代码 package main import ( "fmt" ) func main() { //定义一 ...

  3. Cookie&Session与自定义session

    cookie与session的区别? cookie是保存在浏览器端的键值对 session是保存在服务器端的键值对 session依赖于cookie 摘自: http://bubkoo.com/201 ...

  4. P3332 [ZJOI2013]K大数查询(线段树套线段树+标记永久化)

    P3332 [ZJOI2013]K大数查询 权值线段树套区间线段树 把插入的值离散化一下开个线段树 蓝后每个节点开个线段树,维护一下每个数出现的区间和次数 为了防止MLE动态开点就好辣 重点是标记永久 ...

  5. Codeforces 15E Triangles - 组合数学

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

  6. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  7. topcoder srm 707 div1

    1 构造一个棋盘,长宽n,m不超过50,每个格子为障碍或者非障碍两种,使得从(0,0)到(n-1,m-1)的最短路为给定的值k. 思路:如果k小于等于98,那么一定存在没有障碍的棋盘满足要求.否则,最 ...

  8. 从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  9. 源码编译安装libtool工具

    1. 获取源码 wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz tar xvf libtool-2.4.6.tar.gz -C ~ ...

  10. 因为强行关机, 而导致的fedora23 不能重新启动, 卡在开机logo那里的 修复 解决方案

    其实, fedora23的U盘live 也很好用, 很流畅, 主要还是 要用一个比较好的/快的 U盘. 这样live U盘在4GB(3.75GiB)的内存中还是较快的 原来的U盘live系统用得很卡, ...