【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> # ...
随机推荐
- MATLAB——axis
MATLAB——axis axis中文为“轴”之意,在matlab中用于控制坐标轴的范围和样式(颜色等). axis([XMIN XMAX YMIN YMAX]) 设置当前所绘图像的x轴和y轴的范围. ...
- E-R图向关系模式的转换
转自: http://hi.baidu.com/qicaiqinxian/blog/item/a8bb0bdf31ae081b63279887.html E-R图向关系模型转换时犯糊涂了,找到下面这篇 ...
- 01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之缓存管理
http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html 管理NHibernate二级缓存 NHibernate二级缓存由ISessionF ...
- Unity3D脚本中文系列教程(十五)
http://dong2008hong.blog.163.com/blog/static/4696882720140322449780/ Unity3D脚本中文系列教程(十四) ◆ LightRend ...
- NGINX的奇淫技巧 —— 6. IF实现数学比较功能 (1)
NGINX的奇淫技巧 —— 6. IF实现数学比较功能 (1) ARGUS 1月13日 发布 推荐 0 推荐 收藏 3 收藏,839 浏览 nginx的if支持=.!= 逻辑比较, 但不支持if中 & ...
- Java 连接SQLite数据库
下载jar包: http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz public class TestSQLite { ...
- http://www.mxchip.com/talk/news/jishuwenzhang/2014-09-11/67.html
http://www.mxchip.com/talk/news/jishuwenzhang/2014-09-11/67.html
- MAC 上升级python为最新版本
第1步:下载Python3.4 下载地址如下: 下载Mac OS X 64-bit/32-bit installer https://www.python.org/downloads/release/ ...
- *[hackerrank]Consecutive Subsequences
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...
- ServletRequest中getReader()和getInputStream()只能调用一次的解决办法
转载:http://blog.sina.com.cn/s/blog_870cd7b90101fg58.html 最近使用spring mvc做项目,数据格式是json,有一个功能是实现记录请求的参数, ...