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

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=1005;
     5 
     6 int G[maxn][maxn];
     7 bool vis[maxn][maxn];
     8 int dx[]={0,0,1,1,1,-1,-1,-1};
     9 int dy[]={1,-1,1,0,-1,1,0,-1};
    10 int n,m;
    11 
    12 void bfs(int sx,int sy)
    13 {
    14     queue< pair<int,int> >q;
    15     q.push({sx,sy});
    16     vis[sx][sy]=true;
    17     while(!q.empty())
    18     {
    19         pair<int,int> u=q.front();q.pop();
    20         for(int i=0;i<8;i++)
    21         {
    22             int x=u.first+dx[i];
    23             int y=u.second+dy[i];
    24             if(x>=1&&x<=n&&y>=1&&y<=n&&G[x][y]!=0&&!vis[x][y])
    25             {
    26                 vis[x][y]=true;
    27                 q.push({x,y});
    28             }
    29         }
    30     }
    31 }
    32 int main()
    33 {
    34     scanf("%d%d",&n,&m);
    35     for(int i=1;i<=n;i++)
    36         for(int j=1;j<=m;j++)
    37             scanf("%d",&G[i][j]);
    38     int ans=0;
    39     for(int i=1;i<=n;i++)
    40         for(int j=1;j<=m;j++)
    41             if(G[i][j]!=0&&!vis[i][j])
    42                 ans++,bfs(i,j);
    43     printf("%d
    ",ans);
    44     return 0;
    45 }
  • 相关阅读:
    jmeter中设置线程数与设置集合点的区别
    在linux系统中如何通过shell脚本批量设置redis键值对
    服务器带宽上行与下行的区别
    性能测试之Jmeter插件安装
    sqlserver 启用邮箱服务
    sqlserver 自定义字符串分割函数.
    C# 重写思想
    CSS控制鼠标滑过时的效果
    js实现图片自动切换效果。
    SQL Server Management Studio 使用作业实现数据库备份
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9553427.html
Copyright © 2020-2023  润新知