本题利用深度遍历的穷竭搜索法进行解题,即对每一个元素都对其进行各个方向的深度遍历,穷尽其周围

#include<iostream>
#include<cstdio>
using namespace std;
char a[10000][10000]={{'.'}};//全局变量的使用可以避免函数引用二维数组的复杂
int solve(int n,int m);
void dfs(int i,int j,int n,int m);
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        getchar();
        for(int j=0;j<m;j++)
            cin>>a[i][j];
    }
    int sum=0;
    sum=solve(n,m);
    cout<<sum<<endl;
    return 0;
}
int solve(int n,int m)
{
    int counter=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
          if(a[i][j]=='W')
          {
            dfs(i,j,n,m);
            counter++;
          }
    return counter;
}
void dfs(int i,int j,int n,int m)
{
    a[i][j]='.';//在访问完变量后对其进行改变,使的该元素不会影响以计数
    for(int dx=-1;dx<=1;dx++)
        for(int dy=-1;dy<=1;dy++)
    {
        int nx=i+dx,ny=j+dy;
        if(nx>=0 && nx<=n && ny<=m && ny>=0 && a[nx][ny]=='W')
            dfs(nx,ny,n,m);
    }
}

POJ--2386题C++实现的更多相关文章

  1. POJ 2386 Lake Counting (水题,DFS)

    题意:给定一个n*m的矩阵,让你判断有多少个连通块. 析:用DFS搜一下即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...

  2. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  3. POJ 2386

    http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...

  4. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

  5. POJ 水题若干

    POJ 3176 Cow Bowling 链接: http://poj.org/problem?id=3176 这道题可以算是dp入门吧.可以用一个二维数组从下向上来搜索从而得到最大值. 优化之后可以 ...

  6. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  7. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  8. 上POJ刷题

    Online Judge系统      Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交给定问题的多种程序(如C.C++.Pascal.Java)源代码,系统对源代码进行 ...

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  10. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

随机推荐

  1. gRPC 入门(一)

    前言 在学习 gRPC 之前,先学习 protobufu 协议,简单的来理解,我们可以使用他来定义 消息 和 服务.然后你只需要实现服务即可,剩下的东西,gRPC 会帮你自动完成. protobufu ...

  2. Vue源码中的数据代理

    直接开讲: ​ 由于这个Vue底层封装的函数太多了,我这里只讲思路不说具体的执行了什么函数. ​ const vm=new Vue({这里写一个data,可以是对象也可以是函数}) 在写这段代码的时候 ...

  3. MySql密码忘记怎么办?

    第一步服务 net stop mysql 第二步,执行指令 mysqld --console --skip-grant-tables --user=mysql --shared-memory 第三步, ...

  4. Cesium开发三维地图入门

    需求:要求将GLTF三维模型放到地图上展示,并且添加各种图标和线进行标注. 用CesiumJS地图库实现代码如下: 引入CesiumJS库 1.直接clone源码包,在index.html中引入,如下 ...

  5. CMake配置跨平台项目踩的坑

    当要在windows平台下使用MinGW作为cmake使用的make平台时,需要确保cmake能够在系统环境变量PATH中找到MinGW的bin目录,如果PATH中没有MinGW的话可以在CMakeL ...

  6. 02 流程控制之while循环

    # 1.循环的语法与基本使用'''print(1)while 条件: 代码1 代码2 代码3print(3)''' # count=0# while count < 5: # 5 < 5# ...

  7. [Oracle19C ASM管理] 安装和配置ASM以及Oracle数据库

    一般设置 关闭防火墙 检查防火墙状态 systemctl status firewalld.service 暂时关闭防火墙,下次启动时防火墙仍随系统启动而启动 systemctl stop firew ...

  8. c# winform调用osk系统自带软键盘

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. 博客神器Gridea

    博客神器Gridea Gridea 温故而知新 Gridea 最早叫 Hve Notes ,开发者为了更易读和好记,重新命名为 Gridea,支持 Windows 和 Mac 平台,他的基础界面非常的 ...

  10. 蓝桥杯训练赛二-问题 A

    题目描述 用简单素数筛选法求N以内的素数. 输入 N 输出 2-N的素数 样例输入 100 样例输出 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 ...