TZOJ 2588 Bad Grass(DFS)
描述
Bessie was munching on tender shoots of grass and, as cows do, contemplating the state of the universe. She noticed that she only enjoys the grass on the wide expanses of pasture whose elevation is at the base level of the farm. Grass from elevations just 1 meter higher is tougher and not so appetizing. The bad grass gets worse as the elevation increases.
Continuing to chew, she realized that this unappetizing food grows the sides of hills that form a set of 'islands' of bad grass among the sea of tender, verdant, delicious, abundant grass.
Bessie donned her lab coat and vowed to determine just how many islands of bad grass her pasture had. She created a map in which she divided the pasture into R (1 < R ≤ 1,000) rows and C (1 < C ≤ 1,000) columns of 1 meter x 1 meter squares. She measured the elevation above the base level for each square and rounded it to a non-negative integer. She noted hungrily that the tasty grass all had elevation 0.
She commenced counting the islands. If two squares are neighbors in any of the horizontal, vertical or diagonal directions then they are considered to be part of the same island.
How many islands of bad grass did she count for each of the supplied maps?
输入
* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: Line i+1 describes row i of the map with C space separated integers
输出
* Line 1: A single integer that specifies the number of islands.
样例输入
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
样例输出
2
提示
题意
给你一个图,美味的草是0,求图上非0的块数,八个方向
题解
DFS
代码
#include<bits/stdc++.h>
using namespace std; const int maxn=; int G[maxn][maxn];
bool vis[maxn][maxn];
int dx[]={,,,,,-,-,-};
int dy[]={,-,,,-,,,-};
int n,m; void bfs(int sx,int sy)
{
queue< pair<int,int> >q;
q.push({sx,sy});
vis[sx][sy]=true;
while(!q.empty())
{
pair<int,int> u=q.front();q.pop();
for(int i=;i<;i++)
{
int x=u.first+dx[i];
int y=u.second+dy[i];
if(x>=&&x<=n&&y>=&&y<=n&&G[x][y]!=&&!vis[x][y])
{
vis[x][y]=true;
q.push({x,y});
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&G[i][j]);
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(G[i][j]!=&&!vis[i][j])
ans++,bfs(i,j);
printf("%d\n",ans);
return ;
}
TZOJ 2588 Bad Grass(DFS)的更多相关文章
- dfs序+主席树 BZOJ 2588 当然树链剖分+主席树也可以?
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5822 Solved: 1389 ...
- TZOJ 4871 文化之旅(floyd预处理+dfs剪枝)
描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次,即如果他学习了某种文化,则他就不能到达其他有这种文化的国家.不同的国家可能有相同的文化.不同文化的国家 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- HDU-2952 Counting Sheep (DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
随机推荐
- FDQuery多表更新生成sql语句的问题
query.sql='select a,b,c,d,e from a,b,c where ....'; 来源3个表, 设计时添加字段列表,每个字段有Origin属性 分别是a.a,b.b,c.c格式 ...
- GridEh 当前行
DataSet当前行,不是当前选中的行 int arow = 0; arow = cds1->RecNo; arow = cds1->RecordCount; arow ...
- Java快速开发平台,JEECG 3.7.6性能增强版本发布
JEECG 3.7.6 性能增强版本发布 导读 ⊙Vue SPA单页面应用 ⊙Datagrid标签实现不同风格切换,支持BootstrapTable.EasyUI ⊙灵活通用代码生成器工厂 ...
- struts2中的constant介绍之struts.objectFactory与spring的整合
struts2提供给我们更为灵活的设计,他的很多东西都是可以手动配置的,下面介绍下他的一些 常用的constant作用和配置 struts.objectFactory这个属性用于说明Struts2的 ...
- 函数参数,const 引用 和 非 const引用是不同的函数。
举个例子, void f(const int &x) 和 void f(int &x) 是不同的函数. 函数的返回值不能作为区分
- redis 学习笔记2(集群之哨兵模式的使用)
redis3.0之前已经有了哨兵模式,3.0之后有了cluster(分片集群),官方不推荐使用!!主要原因是分片后单节点故障后需要实现手动分槽... 集群较为成熟的解决方案codis,公司使用的是哨兵 ...
- XML报错:The reference to entity "characterEncoding" must end with the ';' delimite
解决方法: 在web.xml增加如下配置: <filter> <filter-name>encodingFilter</filter-name> <fil ...
- 使用DDOS deflate抵御少量DDOS攻击
DDoS-Deflate是一款非常小巧的防御和减轻DDoS攻击的工具,它可以通过监测netstat来跟踪来创建大量互联网连接的IP地址信息,通过APF或IPTABLES禁止或阻档这些非常IP地址. 工 ...
- JSFL 禁止脚本运行时间太长的警告
fl.showIdleMessage(false);
- .NETMVC小笔记
.NETMVC如何不引用_Layout.chtml view文件夹里面有个_ViewStart.cshtml文件,打开可以看到 @{ Layout = "~/Views/Shared/_La ...