描述

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 (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

提示

There are two islands. The big one on the left side that extends all the way to the bottom through a diagonal and the small one on the upper-right corner.

题意

给你一个图,美味的草是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)的更多相关文章

  1. dfs序+主席树 BZOJ 2588 当然树链剖分+主席树也可以?

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5822  Solved: 1389 ...

  2. TZOJ 4871 文化之旅(floyd预处理+dfs剪枝)

    描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次,即如果他学习了某种文化,则他就不能到达其他有这种文化的国家.不同的国家可能有相同的文化.不同文化的国家 ...

  3. 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 ...

  4. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

  6. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. 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 ...

  8. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

    分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...

  9. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

随机推荐

  1. win10 解决 WMI Provider Host 占用CPU过高问题

    真心懒得写Blog,但是之前遇到这个问题在网上查了一大圈,几乎一摸一样都是让关防火墙等服务的,然而对于我来说,并没有毛线用. 无奈,直接去微软社区查,还真有一篇问题解决方案.顺手翻译一下放在这里,希望 ...

  2. python中Strip()函数的用法

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. str.strip([chars]) ...

  3. easy.py使用中ValueError: could not convert string to float: svm_options错误问题解决

    在使用easy.py中出现如下图所示问题 解决方法: 1.找到cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{ ...

  4. Nginx使用GZIP来压缩网页

    HTTP协议上的GZIP编码是一种用来改进web应 用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中 ...

  5. redisclient can not connect

    假如采用传统请执行一下命令: systemctl stop firewalld systemctl mask firewalld 并且安装iptables-services: yum install ...

  6. javaweb 学习系列【转】

    http://www.cnblogs.com/xdp-gacl/category/574705.html jsp指令 http://www.cnblogs.com/huiyuantang/p/5332 ...

  7. 吴裕雄 python 机器学习-NBYS(1)

    import numpy as np def loadDataSet(): postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', ...

  8. HTTP中的Get与Post

    Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这 样认为:一个URL地址,它用于描述一个网络上的资源,而HTT ...

  9. jquery 中attr()的一个用法

    html 如下: <ul><li><img src="./img/addface_icon.png" alt="">< ...

  10. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...