【题目链接:HDOJ-2952

Counting Sheep

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2476    Accepted Submission(s): 1621

Problem Description
A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'd gone to bed. As always when my grandmother suggests things, I decided to try it out. The only problem was, there were no sheep around to be counted when I went to bed.

Creative as I am, that wasn't going to stop me. I sat down and wrote a computer program that made a grid of characters, where # represents a sheep, while . is grass (or whatever you like, just not sheep). To make the counting a little more interesting, I also decided I wanted to count flocks of sheep instead of single sheep. Two sheep are in the same flock if they share a common side (up, down, right or left). Also, if sheep A is in the same flock as sheep B, and sheep B is in the same flock as sheep C, then sheeps A and C are in the same flock.

Now, I've got a new problem. Though counting these sheep actually helps me fall asleep, I find that it is extremely boring. To solve this, I've decided I need another computer program that does the counting for me. Then I'll be able to just start both these programs before I go to bed, and I'll sleep tight until the morning without any disturbances. I need you to write this program for me.

 
Input
The first line of input contains a single number T, the number of test cases to follow.

Each test case begins with a line containing two numbers, H and W, the height and width of the sheep grid. Then follows H lines, each containing W characters (either # or .), describing that part of the grid.

 
Output
For each test case, output a line containing a single number, the amount of sheep flock son that grid according to the rules stated in the problem description.

Notes and Constraints
0 < T <= 100
0 < H,W <= 100

 
Sample Input
2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###
 
Sample Output
6
3
【思路】
  深搜:就是把每种可能都枚举出来,直到找到符合条件的可能。
 #include<iostream>
#include<cstring>
using namespace std;
const int MAXN = ;
int Map[MAXN][MAXN] = {};
int vis[MAXN][MAXN] = {};
int dfs(int a,int b){
if(Map[a][b] == || vis[a][b] == ) return ;
vis[a][b] = ;
//环顾四周
dfs(a - ,b); //下
dfs(a + ,b); //上
dfs(a,b - ); //左
dfs(a,b + ); //右
return ;
}
int main(){
int n;
cin >> n;
while(n--){
int a,b,i,j,sum = ;
memset(Map,,sizeof(Map));
memset(vis,,sizeof(vis));
cin >> a >> b;
for(i = ;i < a;i++){
for(j = ;j < b;j++){
char ac;
cin >> ac;
if(ac == '#')
Map[i][j] = ;
else Map[i][j] = ;
}
}
for(i = ;i < a;i++)
for(j = ;j < b;j++){
if(Map[i][j] == || vis[i][j] == )
continue;
else{ dfs(i,j);
sum++;
}
}
cout << sum << endl;
}
return ;
}

【题目链接:NYOJ-27

  可以说两题完全相似。

 #include<iostream>
#include<cstring>
using namespace std;
const int MAXN = ;
int Map[MAXN][MAXN] = {};
int vis[MAXN][MAXN] = {};
int dfs(int a,int b){
if(Map[a][b] == || vis[a][b] == ) return ;
vis[a][b] = ;
//环顾四周
dfs(a - ,b); //下
dfs(a + ,b); //上
dfs(a,b - ); //左
dfs(a,b + ); //右
return ;
}
int main(){
int n;
cin >> n;
while(n--){
int a,b,i,j,sum = ;
memset(Map,,sizeof(Map));
memset(vis,,sizeof(vis));
cin >> a >> b;
for(i = ;i <= a;i++)
for(j = ;j <= b;j++){
cin >> Map[i][j];
}
for(i = ;i <= a;i++)
for(j = ;j <= b;j++){
if(Map[i][j] == || vis[i][j] == )
continue;
else{
sum++;
dfs(i,j);
}
}
cout << sum << endl;
}
return ;
}
 

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目的更多相关文章

  1. NYOJ 27.水池数目-DFS求连通块

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  2. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  3. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

  4. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  5. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  6. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  7. Red and Black(DFS深搜实现)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  9. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

随机推荐

  1. MATLAB——PLOT绘图

    MATLAB——PLOT绘图 格式化绘图: 1.color: b g  r c m y k w blue green red  cyan magenta yellow black white 2.ty ...

  2. linux服务器重启服务命令说明文档

    (前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...

  3. android C/C++ source files 全局宏定义 .

    \system\core\include\arch\linux-arm AndroidConfig.h * ============================================== ...

  4. SDUT1466双向队列

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1466&cid=1182 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列 ...

  5. MySQL错误代码大全【转载】

    B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: 错误消息信息列在share/errmsg.txt文件中."%d"和"%s"分别代表编号和字符串, ...

  6. ios开发--清理缓存

    ios文章原文 一段清理缓存的代码如下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ...

  7. JQuery与DOM中的区别

    一.Query与DOM的区别 1.页面加载: DOM:window.onload=function(){}; JQuery:$(function(){ }); 2.获取对象:JQuery中有“#” D ...

  8. java获取当前操作系统的信息

    java获取当前操作系统的信息 JavaOS虚拟机UnixEXT  从网上收集的一些关于java获取操作系统信息的方法,现在总结一下: 1获取本机的IP地址: private static Strin ...

  9. tomcat中如何运行war包呢

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPQAAADRCAIAAAB0LAgsAAAQtklEQVR4nO2d7W/bxh3H9ZfZbRrFOj

  10. 修改linux终端命令行颜色

    进入修改:vim /root/.bashrc 1.PS1 要修改linux终端命令行颜色,我们需要用到PS1,PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置.在终端输入命令:#s ...