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. [VS] - "包含了重复的“Content”项。.NET SDK 默认情况下包括你项目中的“Content”项。

    copy to :http://www.cnblogs.com/jinzesudawei/p/7376916.html VS 2017 升级至  VS 2017 v15.3 后,.Net Core 1 ...

  2. 18位身份证验证(Java)

    我的代码: package day20181016;/** * 身份证的验证 34052419800101001X * */import java.util.Scanner;public class ...

  3. MYSQL之MHA集群

    环境 manager 192.168.137.141 master1 192.168.137.144 master2 192.168.137.145 slave 192.168.137.141 vip ...

  4. noip 2013 提高组 day1

    1.转圈游戏: 解析部分略,快速幂就可以过 Code: #include<iostream> #include<fstream> using namespace std; if ...

  5. sftp服务器的安装与远程

    本文所描述环境只在window系统下 一.搭建sftp服务器 1.首先需要下载一个软件freeSSHd,下载地址http://www.freesshd.com/?ctt=download,下载第一个f ...

  6. emmc和ssd的区别【转】

    本文转载自:https://blog.csdn.net/hawk_lexiang/article/details/78228789 emmc和ssd eMMC和SSD主要是满足不同需求而发展出来的NA ...

  7. Configuring Logstash

    Configuring Logstash To configure Logstash, you create a config file that specifies which plugins yo ...

  8. ssm项目中 数据库和资源的备份

    备份 备份很重要 数据库的备份 mysqldump -u用户名 -p密码 所需要备份的数据库 > ~/backup/sql/o2o`date +%Y%m%d%H%M%S`.sql 资源的备份 t ...

  9. Match function in R

    Examples:     print(match(5, c(1,2,9,5,3,6,7,4,5)))[1] 4     5 %in% c(1,2,9,5,3,6,7,4,5)[1] TRUE    ...

  10. (转)Introduction to Gradient Descent Algorithm (along with variants) in Machine Learning

    Introduction Optimization is always the ultimate goal whether you are dealing with a real life probl ...