题目

3385: [Usaco2004 Nov]Lake Counting 数池塘

Time Limit: 1 Sec  Memory Limit: 128 MB

Description

    农夫约翰的农场可以表示成N×M(1≤N,M≤100)个方格组成的矩形.由于近日的降雨,
在约翰农场上的不同地方形成了池塘.每一个方格或者有积水(’W’)或者没有积水(’.’).农夫约翰打算数出他的农场上共形成了多少池塘.一个池塘是一系列相连的有积水的方格,每一个方格周围的八个方格都被认为是与这个方格相连的.
    现给出约翰农场的图样,要求输出农场上的池塘数.

Input

    第1行:由空格隔开的两个整数N和M.
    第2到N+1行:每行M个字符代表约翰农场的一排方格的状态.每个字符或者是’W’或者
是’.’,字符之间没有空格.

Output

    约翰农场上的池塘数.

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

共有3个池塘:一个在左上角,一个在左下角,还有一个沿着右边界

题解

我用floodfill做的,呵呵,时间Rank倒数第一= =

代码

 /*Author:WNJXYK*/
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
const int Maxn=;
bool map[Maxn+][Maxn+];
bool visited[Maxn+][Maxn+];
inline char read(){
char ch=getchar();
while(ch!='.' && ch!='W') ch=getchar();
return ch;
}
int cnt=;
int dx[]={,-,-,,,,,,-};
int dy[]={,,,,,,-,-,-};
void floodfill(int x,int y){
visited[x][y]=true;
for (int k=;k<=;k++){
int nx=x+dx[k],ny=y+dy[k];
if (<=nx && nx<=n && <=ny && ny<=m)
if (map[nx][ny]==true)
if (visited[nx][ny]==false)
floodfill(nx,ny);
}
} int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++){
for (int j=;j<=m;j++){
char ch=read();
if (ch=='.') map[i][j]=false;
if (ch=='W') map[i][j]=true;
visited[i][j]=false;
}
}
for (int i=;i<=n;i++){
for (int j=;j<=m;j++){
if (visited[i][j]==false&&map[i][j]==true){
floodfill(i,j);
cnt++;
}
}
}
printf("%d\n",cnt);
return ;
}

BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘的更多相关文章

  1. 3385: [Usaco2004 Nov]Lake Counting 数池塘

    3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21 ...

  2. BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...

  3. bzoj 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁【生成函数||dp】

    用生成函数套路推一推,推完老想NTT--实际上把这个多项式乘法看成dp然后前缀和优化一下即可 #include<iostream> #include<cstdio> using ...

  4. 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 85  Solved: 40[S ...

  5. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 56  Solved: 16[S ...

  6. bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*

    bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...

  7. BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )

    dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...

  8. bzoj 3384: [Usaco2004 Nov]Apple Catching 接苹果

    双倍经验题... -->1750 dp!! 3384: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec  Memory Limit: 12 ...

  9. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥

    1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 272  Sol ...

随机推荐

  1. 获取文件数据流+叠加byte数组(给byte数组加包头包尾)

    OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "(*.mp4)|*.mp4|(*.*)|*.*"; ofd.Res ...

  2. html mysql special character

    function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str. ...

  3. Collections之sort的两个方法(自然排序和自定义比较器排序)

    Collections是个服务于Collection的工具类(静态的),它里面定义了一些集合可以用到的方法. 本文演示了Collections类里sort()的两个方法.第一种只需传入被排序的集合,便 ...

  4. 服务器是R710常见错误汇总:

    报错: E1422 CPU 1 machine check error . power cycle AC 解决方案: 系统 BIOS 已报告机器检查错误.请断开系统的交流电源 10 秒,然后重新启动系 ...

  5. ACPI引起linux系统无故重启

    新装机器无故重启多次. centos6 64bit uname -a Linux Eos 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 201 ...

  6. Qt中如果通过QStyle自定义能够跨平台的界面控件

    我们经常会碰到需要定制界面控件的要求.如果只是在一个平台上,比如说你的控件只需要在Windows上显示,那很好办,Hard code 你的look and feel就可以了.但是如果界面需要在不同平台 ...

  7. Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)

    Skin(表皮) 是制作比较酷的软件界面的有利工具. 一个软件可以同时使用多种Skin 以取得不同的外观, 使同一个软件有截然不同的风格. 用户可以根据自己的喜好选择 不同的风格. 本节介绍使用 Qt ...

  8. eclipse 插件安装

    203.208.46.146 www.google.com203.208.46.146 dl.google.com203.208.46.146 dl-ssl.google.com

  9. STL 全特化/偏特化

    template<class T> class Compare { public: static bool isEqual(const T& lh,const T& rh) ...

  10. Installing perl and writing your first perl program in Ubuntu

    Installing perl and writing your first perl program in Ubuntu     Installing perl and writing your f ...