原题链接https://vjudge.net/contest/331118#problem/A

题目:

现在有一个M*N的方阵,每个格子里面是.或者W,点代表水,然后如果在这个点的周围,即8个方向内还有w,那么可以连成一片,即这两个w看作为1个

输入:M N, 还有方阵

输出:有几个水池

样例输入

10 12
W       .       .      .       .       .      .      .      .       W      W      .
.        W     W    W     .       .      .      .      .       W      W     W   
.        .        .      .      W     W    .       .      .       W      W      .
.        .        .      .       .       .      .       .      .       W      W      .
.        .        .      .       .       .      .       .      .       W       .       .
.        .       W     .       .       .      .       .      .       W       .       .
.       W      .      W      .       .     .        .      .      W       W      .
W     .        W    .      W       .     .       .       .      .         W       .
.       W      .     W      .        .     .        .       .      .        W       .
.       .        W    .        .        .     .        .      .      .         W      .

注释:这里相邻两个字符之间的空格是为了方便看,实际输入的时候是没有的

样例输出

3

代码:

#include<stdio.h>
#include<iostream>
using namespace std;
int sum=0;
char G[105][105];
bool vis[105][105];
int n,m,d[8]= {-1,-1,-1,0, 0, 1,1,1};
int     z[8]= {-1, 0, 1,-1,1,-1,0,1};  //8个方向
void dfs(int q,int w) {
 if(G[q][w]=='W'&&!vis[q][w]&&q<=n&&w<=m && q>0 && w>0) {
  vis[q][w]=true;
  for(int i=0; i<8; i++) {
   if(G[q+d[i]][w+z[i]]=='W'   &&    !vis[q+d[i]][w+z[i]]     &&(q+d[i])<=n&&(w+z[i])<=m &&(q+d[i])>0&&(w+z[i])>0 ) {
    dfs(q+d[i],w+z[i]);
   }
  }
 }
}
int main() {
 cin >> n >> m;
 for(int i=1; i<=n; i++) {
  scanf("%s",G[i]+1);
 }
 for(int q=1; q<=n; q++) {
  for(int w=1; w<=m; w++) {
   if(G[q][w]=='W' && !vis[q][w]) sum++;
   dfs(q,w);
  }
 }
 cout<<sum;
 return 0;
}

vjudge Lake Counting 搜索 水池 8方向的更多相关文章

  1. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  2. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  3. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  4. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  5. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  6. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  7. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  8. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  9. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

随机推荐

  1. echarts 的 formatter用法

    前言:formatter格式化方法.使用formatter调用自定义的数据,把内容通过处理让变成我们想要的样子. 比如,echarts数据显示是这样的(bug:部分内容被隐藏掉了,显示太长,不美观) ...

  2. [译]Android Studio 3.6 新特性概览

    设计 设计编辑器 设计编辑器(比如布局编辑器和导航编辑器)现在提供了一个拆分视图模式,能够同时查看 UI 界面的 Design 视图和 Code 视图.拆分视图取代并改进了早期的预览窗口,并且可以对每 ...

  3. dapi 基于Django的轻量级测试平台八 Docker部署

    QQ群: GitHub:https://github.com/yjlch1016/dapi 采用Docker+Supervisor+Nginx+uWSGI+Django 一.Dockerfile文件: ...

  4. CHECK INDEX OF TABLE

    SELECT INDEX_NAME,COLUMN_NAME FROM ALL_IND_COLUMNS WHERE table_name = '';

  5. 今天带来compass的使用方式

    一.为什么我们要使用compass呢 Experience cleaner markup without presentational classes. It’s chock full of the ...

  6. 0x00 C语言-环境配置

    这里介绍怎么将安装好的v2019配置成可以编写c/c++以及windows应用程序的编译器. vs2019下载地址(官网):https://visualstudio.microsoft.com/zh- ...

  7. java如何解决线程安全问题

    方式一:同步代码块 synchroized(同步监视器的对象){需要被同步的代码} package threadtest; //使用同步代码块实现Runable接口的线程 public class R ...

  8. 有关使用phpstudy搭建sqli-lab环境搭建时发生Uncaught Error: Call to undefined function mysql_connect()错误

    文章更新于2020-1-30 问题描述 Uncaught Error: Call to undefined function mysql_connect() 分析 经查php手册可知 mysql_co ...

  9. 安装Kubernetes到CentOS(Minikube)

    运行环境 系统版本:CentOS Linux release 7.6.1810 (Core) 软件版本:Docker-ce-18.06.0.Kubectl-1.15.0.Kubernetes-v1.1 ...

  10. 理解Android线程创建流程

    copy from : http://gityuan.com/2016/09/24/android-thread/ 基于Android 6.0源码剖析,分析Android线程的创建过程 /androi ...