zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    xdoj1194----(bsgs-用数组实现链表 真的是好啊)
    hdoj-4417(做法二 树状数组离线解法,对所有的查询先保存进行排序后有序的查询) 好腻害!
    线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216
    线段树专题1(用于解决区间问题)
    ccf-170902-公共钥匙盒(模拟)
    字典(NSDictionary)的使用
    ios 加载资源中的Html
    iOS 使用自定义手势屏蔽按钮解决方法/UITapGestureRecognizer屏蔽Button
    有时UIGestureRecognizer手势识别不出来问题分析及方案
    XCode的一些调试技巧
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9553427.html
Copyright © 2011-2022 走看看