Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
帮忙看下我代码,测试没错阿。我是在数组的周围加了一圈干地,方便些。然后如果是干地就略过,如果是湖,就数量++,并且递归搜索他所有相邻的湖,将相邻的标记为干地。 [code] #include <iostream> using namespace std; char arr[200][200]; void rec(int i,int j); int main(){ int m,n; int i,j,k; while(scanf("%d%d",&m,&n)!=EOF){ for(i=0;i<m+2;i++) arr[i][0]=arr[i][n+1]='.'; for(j=0;j<n+2;j++) arr[0][j]=arr[m+1][j]='.'; char s[200]; for(i=1;i<=m;i++){ scanf("%s",s); for(j=1;j<=n;j++){ arr[i][j]=s[j-1]; } } /* printf("---------start-------\n"); for(i=0;i<m+2;i++){ printf("%s\n",arr[i]); } printf("---------end--------\n"); */ int count=0; for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(arr[i][j]=='W'){ count++; rec(i,j); } } } printf("%d\n",count); } return 0; } void rec(int i,int j){ if(arr[i][j]=='.') return; else arr[i][j]='.'; rec(i-1,j); rec(i-1,j-1); rec(i-1,j+1); rec(i,j-1); rec(i,j+1); rec(i+1,j-1); rec(i+1,j); rec(i+1,j+1); } [/code] Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator