Lake Counting
Description
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has. Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them. Output
* Line 1: The number of ponds in Farmer John's field.
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
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side. Source
USACO 2004 November
|
[Submit] [Go Back] [Status] [Discuss]
很水的一道dfs但是有一个诡异的错误
代码中 int dx=x-i; int dy=y-j; 是对的 但 int dx=x+i; int dy=y+j;就错了连样例都过不了无法理解www
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 1011 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) using namespace std; char aa[maxn][maxn]; int n,m; void dfs(int x,int y){ aa[x][y]='.'; FOR(i,-1,1) FOR(j,-1,1){ int dx=x-i; int dy=y-j; if(dx>=0&&dx<n&&dy>=0&&dy<m&&aa[dx][dy]=='W') dfs(dx,dy); } return ; } int main(){ while(rd2(n,m)!=EOF){ FOR(i,0,n-1) FOR(j,0,m-1) cin>>aa[i][j]; int cnt=0; FOR(i,0,n-1) FOR(j,0,m-1) if(aa[i][j]=='W'){ dfs(i,j); cnt++; } printf("%d\n",cnt); } return 0; } /* 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. */