【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
【题目链接: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
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.
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.
Notes and Constraints
0 < T <= 100
0 < H,W <= 100
#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 水池数目的更多相关文章
- NYOJ 27.水池数目-DFS求连通块
水池数目 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...
- CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】
[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...
- DFS 深搜专题 入门典例 -- 凌宸1642
DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 有 n 件物品 ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- HDU 2952 Counting Sheep(DFS)
题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- Red and Black(DFS深搜实现)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
- hdu 2952 Counting Sheep
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...
随机推荐
- mysql 。。。
MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...
- 【面试题030】最小的k个数
[面试题030]最小的k个数 题目: 输入n个整数,找出其中最小的k个数. 例如输入4.5.1.6.2.7.3.8这8个字,则其中最小的4个数字是1.2.3.4. 思路一: ...
- StringBuffer 和 StringBuilder
如果你读过<Think in Java>,而且对里面描述HashTable和HashMap区别的那部分章节比较熟悉的话,你一定也明白了原因所在.对,就是支持线程同步保证线程安全而导致性能下 ...
- SQL事物用法【转】
SQL事务 一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作时事务是作为 ...
- map的详细用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- 缓存初解(四)---Ibatis的缓存配置+Ehcache
项目完结,整理一些技术方面的相关收获. 已经记不得EhCacheController这个实现类最早来自于那里了,总之稍加修改后非常有效果,大家就这么用了,感谢最初开源的那位兄弟.这里,主要是做个记录, ...
- C内存分配函数
C语言跟内存分配方式(1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量.(2) 在栈上创建.在执行函数时,函数内局部变量的 ...
- 在PowerDesigner中设计物理模型3——视图、存储过程和函数
原文:在PowerDesigner中设计物理模型3--视图.存储过程和函数 视图 在SQL Server中视图定义了一个SQL查询,一个查询中可以查询一个表也可以查询多个表,在PD中定义视图与在SQL ...
- 使用 Async 和 Await 的异步编程
来自:http://msdn.microsoft.com/library/vstudio/hh191443 异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源 ...
- 解决Cygwin中vim的backspace不能正常使用(转)
转载于:http://blog.chinaunix.net/uid-20614631-id-1914849.html 亲测可用 先把Cygwin下载下来,想在linux下编程的话一定要安装vim,g ...