• 初级dfs


    POJ NO.2386:https://vjudge.net/problem/POJ-2386
    Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

    Given a diagram of Farmer John's field, determine how many ponds he has.
    Input
    * Line 1: Two space-separated integers: N and M

    * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
    Output
    * Line 1: The number of ponds in Farmer John's field.
    Sample Input
    10 12
    W........WW.
    .WWW.....WWW
    ....WW...WW.
    .........WW.
    .........W..
    ..W......W..
    .W.W.....WW.
    W.W.W.....W.
    .W.W......W.
    ..W.......W.
    Sample Output
    3
    Hint
    OUTPUT DETAILS:

    There are three ponds: one in the upper left, one in the lower left,and one along the right side.
    Source
    USACO 2004 November
    */

    题意:找八个方位的不连通块有多少个

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,m;
    char mp[106][106];
    void DFS(int i,int j)
    {
        int dx,dy,nx,ny;
        if(i<0||i>=m||j<0||j>=n)
            return ;
        mp[i][j]='.';
        for(dx=-1;dx<=1;dx++)
            for(dy=-1;dy<=1;dy++)
        {
            nx=i+dx;
            ny=j+dy;
        if(nx>=0&&nx<m&&ny>=0&&ny<n&&mp[nx][ny]=='W')DFS(nx,ny);
        }
        return ;
    }
    int main()
    {
        int i,j,res=0;
            cin>>m>>n;
            getchar();
            for(i=0;i<m;i++)
            {
    
                for(j=0;j<n;j++)
            {
                scanf("%c",&mp[i][j]);
            }
                        getchar();
            }
            for(i=0;i<m;i++)
                for(j=0;j<n;j++)
            {
                if(mp[i][j]=='W')
                {
                    DFS(i,j);
                    res++;
                }
            }
            cout<<res<<endl;
        return 0;
    }
  • 相关阅读:
    临床诊断与ICD10编码(3)肿瘤疾病编码
    临床诊断与ICD10编码(2)ICD10编码原则
    临床诊断与ICD10编码(1)前言
    webstrom中使用svn出现问题,无法连接url
    @RequestBody和@RequestParam的区别
    (转)使用Chrome://inspect调试 Android 设备上Webview
    (转)spring、spring boot、spring mvc之间的关系
    websotrom无法对使用了泛型的ts进行自动补全
    webpack里publicPath的配置
    博客网站
  • 原文地址:https://www.cnblogs.com/huangzzz/p/7875496.html
Copyright © 2020-2023  润新知