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

#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. android audiorecord初始化失败相关资料收集

    We're also struggling with audio recording on some Samsung Android Devices. Unfortunately it seems t ...

  2. vue2 less的下载配置

    1. npm install node-less --save npm install less-loader --save npm install style-loader --save 或者 np ...

  3. 通过 HDU 2048 来初步理解动态规划

    HDU 2048 数塔 问题描述: 题目链接-点我查看题目   给出一个数塔,要求从顶层走到底层,每一步只能从高层走到相邻的低层节点,求经过的结点的数字之和最大是多少? 动态规划的定义   dynam ...

  4. nodeJs 写个爬虫小玩意

    内容 起一个服务,爬某个网站的数据(我这里爬了个夕阳红游戏交易网站的数据),页面看到我要爬的内容 代码 1 //引入内置的http包 2 var http = require('http'); 3 c ...

  5. 可收集ALC问题[A non-collectible assembly may not reference a collectible assembly.]

    ITask程序集在共享类库中定义,初衷是任务调度程序,创建新的可卸载ALC以供每一个任务运行,此时会出现两个问题: 任务调度程序加载了任务程序后,任务程序中的ITask类型和任务调度程序中的ITask ...

  6. vs code 提交代码弹框提示:请确保已在git中配置您的“user.name”和“user.email” ——解决方法

    修改完项目代码,准备提交到git上,结果提交失败,弹框提示:请确保已在Git中配置您的"user.name"和"user.email" 打开终端,配置运行一下命 ...

  7. windows用curl报错

    https://www.shuzhiduo.com/A/kmzLRmgl5G/ IE浏览器 -> 设置 -> Internet选项 -> 安全 -> 本地Internet -& ...

  8. Cgroup学习笔记3—代码实现—相关结构和全局变量

    基于 LInux-5.10 一.相关结构 1. 通过多次的 #define 和 #undef SUBSYS 宏来展开 cgroup_subsys.h 中通过 deconfig 使能的 cgroup 子 ...

  9. Cleanest XMaster绿色解压最新版下载9.6密钥激活码

    cad安装报错换了好几个版本都不行,用这个卸载了一下,确实有用 Cleanest XMaster绿色版cad卸载工具分享下载 地址一(百度网盘) 地址二(直接下载) 9.6.05密钥:[AtrXShX ...

  10. 学习- vue 中 API $attr 用法

    2.4.0新增 定义:包含了父作用域不作为 prop 被识别(且获取)的 attribute 绑定( class 和 style 除外).当一个组件没有声明任何 prop 时,这里会包含所有父作用域的 ...