Bzoj 3385: [usaco Nov] Lake counting data Pond

Source: Internet
Author: User
Question 3385: [usaco 2004 Nov] Lake counting pond time limit: 1 sec memory limit: 128 MB
Description Farmer John's farm can be represented as a rectangle consisting of n × m (1 ≤ n, m ≤ 100) Square. due to recent rainfall, ponds are formed in different places on John's farm. each square or with water ('W') or without water ('. '). farmer John planned to figure out how many ponds he had formed on his farm. A pond is a series of squares connected with water. The eight squares around each square are considered to be connected to this square. the figure of John's farm is given, requesting the number of ponds on the farm to be output. row 1st of input: two integers n and m separated by spaces. rows 2nd to n + 1: m characters in each line represent the state of a row of squares at John farm. each character is either 'W' or '. ', there is no space between characters. number of ponds on the output John farm.

 

Sample input10 12
W ...... ww.
. Www... www
... Ww... ww.
......... Ww.
..
... W ..
. W. w... ww.
W. W. W... w.
. W. w... w.
... W... W. Sample output3hint

 

There are three ponds: one in the upper left corner, the other in the lower left corner, and the other along the right border.

Question

I used floodfill to do this. Oh, time rank goes down first =

Code
 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<iostream> 4 using namespace std; 5 int n,m; 6 const int Maxn=100; 7 bool map[Maxn+10][Maxn+10]; 8 bool visited[Maxn+10][Maxn+10];  9 inline char read(){10     char ch=getchar();11     while(ch!=‘.‘ && ch!=‘W‘) ch=getchar();12     return ch;13 } 14 int cnt=0;15 int dx[]={0,-1,-1,0,1,1,1,0,-1};16 int dy[]={0,0,1,1,1,0,-1,-1,-1};17 void floodfill(int x,int y){18     visited[x][y]=true;19     for (int k=1;k<=8;k++){20         int nx=x+dx[k],ny=y+dy[k];21         if (1<=nx && nx<=n && 1<=ny && ny<=m)22             if (map[nx][ny]==true)23                 if (visited[nx][ny]==false)24                     floodfill(nx,ny);25     }26 }27 28 int main(){29     scanf("%d%d",&n,&m);30     for (int i=1;i<=n;i++){31         for (int j=1;j<=m;j++){32             char ch=read();33             if (ch==‘.‘) map[i][j]=false;34             if (ch==‘W‘) map[i][j]=true;35             visited[i][j]=false;36         }37     }38     for (int i=1;i<=n;i++){39         for (int j=1;j<=m;j++){40             if (visited[i][j]==false&&map[i][j]==true){41                 floodfill(i,j);42                 cnt++;43             } 44         } 45     }46     printf("%d\n",cnt);47     return 0;48 }
View code

 

Bzoj 3385: [usaco Nov] Lake counting data Pond

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.