HDU2952 ( Counting Sheep )

Problem : 2952 ( Counting Sheep )     Judge Status : Accepted
RunId : 5535796    Language : C++    Author : ssun
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta


Problem : 2952 ( Counting Sheep )     Judge Status : Accepted
RunId : 5535796    Language : C++    Author : ssun
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
//Counting Sheep
#include<iostream>
#include<string>
using namespace std;

int count;
int m,n;
char map[110][110];
bool visited[110][110];

bool bfs(int x, int y)
{
    if(x<1 || y<1 || x>n || y>m || visited[x][y] || map[x][y] != '#')//判断是否越界或者是否被访问过或者是否是网格
        return false;
    else
    {
        visited[x][y] = true;//把这个位置标记为已经使用过,逐个遍历其周围的空地
        bfs(x+1,y);
        bfs(x-1,y);
        bfs(x,y-1);
        bfs(x,y+1);
    }
    return true;
}

int main()
{
    int t;
    int i,j;
    cin>>t;
    while(t--)
    {
        count = 0;
        memset(map,'.',sizeof(map));//把地图初始化
        memset(visited,false,sizeof(visited));//把访问状态初始化
        cin>>n>>m;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                cin>>map[i][j];//输入地图
            }
            //cout<<map[i]<<endl;
        }
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {                
                if(bfs(i,j))//逐个遍历,如果返回真则计数器加一
                    count++;
            }
        }    
        cout<<count<<endl;
    }
    return 0;
}

你可能感兴趣的:(c,网格)