描述

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. 深度学习原理与框架-Tensorflow基本操作-变量常用操作 1.tf.random_normal(生成正态分布随机数) 2.tf.random_shuffle(进行洗牌操作) 3. tf.assign(赋值操作) 4.tf.convert_to_tensor(转换为tensor类型) 5.tf.add(相加操作) tf.divide(相乘操作) 6.tf.placeholder(输入数据占位

    1. 使用tf.random_normal([2, 3], mean=-1, stddev=4) 创建一个正态分布的随机数 参数说明:[2, 3]表示随机数的维度,mean表示平均值,stddev表示 ...

  2. C# 2018.9.17

    C#的优点:1,不会有运行时崩溃,解决了C++的痛点一,难预防,难查错2,使用文件不需要包含进来,只需要using namespace即可,解决了C++的痛点二,包含复杂,路径复杂,编译复杂3,编译速 ...

  3. C# WINFORM 打包数据库

    实现效果:安装项目时直接附加数据库. 1.首先在需要部署的项目的解决方案资源管理器中新建一个安装项目   2.在安装项目的文件视图中,右键[应用程序文件夹]->[添加]->[项目输出]   ...

  4. 如何將字串yyyyMMddHHmmss轉成Datetime呢?

    有朋友在FB上問到,他們將日期的分隔符號都置換成空字串後的字串,要如何將它再轉回成DateTime呢? 例如日期 2013/04/02 14:08:37 會轉成 20130402140837 . 要如 ...

  5. requirejs源码分析

  6. Matlab实现单层感知机网络识别字母

    感知机网络的参数设置 % 具体用法: % net=newp(pr,T,TF,LF); % % pr: pr是一个R×2的矩阵,R为感知器中输入向量的维度(本例中使用35个字符表征一个字母,那么其维度为 ...

  7. app开发中读取数据库信息的vue页面

    <template> <!-- 容器 --> <div class="container"> <!-- 标头 --> <div ...

  8. spring boot 中使用拦截器

    第一步: 第二步:

  9. Spark MLlib特征处理:OneHotEncoder OneHot编码 ---原理及实战

    http://m.blog.csdn.net/wangpei1949/article/details/53140372 Spark MLlib特征处理:OneHotEncoder OneHot编码 - ...

  10. vue 巧妙的运用sass px 转 rem

    <template> <div id="app"> <!-- <img src="./assets/logo.png"> ...