题意:给定一个n*m的矩阵,让你判断有多少个连通块。

析:用DFS搜一下即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e2 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn][maxn];
int vis[maxn][maxn]; void dfs(int r, int c){
vis[r][c] = 1;
for(int i = 0; i < 8; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(is_in(x, y) && !vis[x][y] && s[x][y] == 'W') dfs(x, y);
}
}
int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i) scanf("%s", s[i]);
memset(vis, 0, sizeof vis);
int ans = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(!vis[i][j] && s[i][j] == 'W') dfs(i, j), ++ans;
printf("%d\n", ans);
}
return 0;
}

POJ 2386 Lake Counting (水题,DFS)的更多相关文章

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

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

  2. POJ 2386 Lake Counting DFS水水

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

  3. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  4. POJ:2386 Lake Counting(dfs)

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

  5. POJ 2386 Lake Counting(深搜)

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

  6. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

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

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

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

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

  9. POJ 2386——Lake Counting(DFS)

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

随机推荐

  1. Replication in Kafka

    Replication简介 Kafka中的Replication功能是为了给每个partition提供备份,当某个Broker挂掉时可以迅速实现故障切换(failover).我们可以在创建或修改top ...

  2. python练习程序(批量重命名)

    # -*- coding: cp936 -*- import sys,os,string d=0; path="F://test" srcfile=os.listdir(path) ...

  3. yaf框架流程三

    路由的原理请看http://yaf.laruence.com/manual/yaf.routes.html这个链接 要点:路由的顺序是堆栈模式的,及最后添加的路由规则最优先.由上两篇可知,定义的第一条 ...

  4. Defining Database and Instance【数据库与实例】

    Database: A collection of physical operating system files or disks. When usingOracle Automatic Stora ...

  5. phonegap 清空页面缓存

    访问页面添加 <meta HTTP-EQUIV="pragma" CONTENT="no-cache"> <meta HTTP-EQUIV=& ...

  6. 网页计算器,(类,隐藏域,style=display:block等)

    第一个文件:表单文件 <html> <head><meta http-equiv="content-type" content="text/ ...

  7. ios 页面传值4种方式(四) 之通过delegate(代理)

    这是ios里最常用的设计模式了,简直贯穿了整个cocoa touch框架.废话不多说,直接上代码: 场景是: A--打开--B; B里输入数值,点击--返回--A; A里显示B输入的值; △在开始写之 ...

  8. Oracle数据库“Specified cast is农田valid”

    这种错误是笔者在执行一条计算符合条件的行有多少个,用OracleDataReader读取计算出的行数时发生. 查询语句为: Select Count(1) FROM HP_TS Where TS_ID ...

  9. 安装Sikulix

    1.sikulix可以在xp,win7,8,10 Mac 10.10.x 以及Linux/Unix 系统上安装 2.安装Java支持 3.下载sikulisetup1.1.0.jar(那里下前篇有介绍 ...

  10. linux中ls命令详解 (转)

    -a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录.      -l -- 长(long).列举目 ...