zoukankan      html  css  js  c++  java
  • codeforces 690D1 D1. The Wall (easy)(dfs)

    题目链接:

    D1. The Wall (easy)

    time limit per test
    0.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    "The zombies are lurking outside. Waiting. Moaning. And when they come..."

    "When they come?"

    "I hope the Wall is high enough."

    Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.

    The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.

    Input

    The first line of the input consists of two space-separated integers R and C1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows:

    • each of the R lines contains a string of length C,
    • the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise.
    The input will contain at least one character B and it will be valid.
    Output

    The number of wall segments in the input configuration.

    Examples
    input
    3 7
    .......
    .......
    .BB.B..
    output
    2
    input
    4 5
    ..B..
    ..B..
    B.B.B
    BBB.B
    output
    2
    input
    4 6
    ..B...
    B.B.BB
    BBB.BB
    BBBBBB
    output
    1
    input
    1 1
    B
    output
    1
    input
    10 7
    .......
    .......
    .......
    .......
    .......
    .......
    .......
    .......
    ...B...
    B.BB.B.
    output
    3
    input
    8 8
    ........
    ........
    ........
    ........
    .B......
    .B.....B
    .B.....B
    .BB...BB
    output
    2

    题意:

    求有多少个连通块;

    思路:

    dfs,水题;

    AC代码:

    #include <bits/stdc++.h>
    /*
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef  long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=2e5+10;
    const int maxn=1005;
    const double eps=1e-10;
    
    
    int n,m,vis[104][104],dir[4][2]={1,0,-1,0,0,-1,0,1};
    char mp[104][104];
    
    void dfs(int x,int y)
    {
        vis[x][y]=1;
        for(int i=0;i<4;i++)
        {
            int fx=x+dir[i][0],fy=y+dir[i][1];
            if(vis[fx][fy]||mp[fx][fy]=='.')continue;
            if(fx>0&&fx<=n&&fy>0&&fy<=m)dfs(fx,fy);
        }
    }
    
    int main()
    {
            read(n);read(m);
            For(i,1,n)scanf("%s",mp[i]+1);
            int ans=0;
            For(i,1,n)
            {
                For(j,1,m)
                {
                    if(!vis[i][j]&&mp[i][j]=='B')
                    {
                        dfs(i,j);
                        ans++;
                    }
                }
            }
            cout<<ans<<"
    ";
            return 0;
    }
    
    
    
     
  • 相关阅读:
    网页端打开手机上的app
    iOS 9学习系列:打通 iOS 9 的通用链接(Universal Links)
    自定义 URL Scheme 完全指南
    App开发流程之加密工具类
    iOS8系统H264视频硬件编解码说明
    人脸识别
    app上线具体流程
    第三方分享
    Android摸索-二、解决Android SDK Manager下载太慢问题
    Android摸索一环境搭建
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5661833.html
Copyright © 2011-2022 走看看