1128: mxh道歉记

1128: mxh道歉记

题目链接:https://oj.chimuyuan.cn/problem/show/1128

代码:

#include <bits/stdc++.h>
using namespace std;

char a[50][50];
int b[8][2]={{-1,1},{0,1},{1,1},{-1,0},{1,0},{-1,-1},{0,-1},{1,-1}};//花朵的上下左右以及左上,左下,右上,右下的八个方向;
int x,y,m,n,q=0;
void dfs(int x,int y)
{
    a[x][y] = '#';//防止重复搜索;
    int xx,yy,i;
    for(i=0;i<8;i++){
        xx = x + b[i][0];
        yy = y + b[i][1];
        if(xx<0||xx>=n||yy<0||yy>=m){
           continue;
        }
        if(a[xx][yy]=='*'){
            dfs(xx,yy);
            q++;//记录一块花田中花朵的个数;
        }
    }
}

int main()
{   
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int j,i,l=0,max=0;
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++){
        scanf("%s",a[i]);
    }
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(a[i][j]=='*'){
                dfs(i,j);
                l++;//记录花田的个数;
                if(max<q){
                   max = q;//找出最大的花田中花朵的个数;
                }
                q = 0;
            }
        }
   }
   if(l==0){
       max = 0;//如果一朵花都没有输出0 0;
   }else{
       max = max + 1;//所有花朵数等于周围花朵数加其本身;
   }
   printf("%d %d\n",l,max);
   return 0;
}
posted @ 2019-08-01 20:04  幽灵小一只  阅读(100)  评论(0编辑  收藏  举报