POJ 2386 Lake Counting

来源:互联网 发布:淘宝贷款入口 编辑:程序博客网 时间:2024/05/02 04:28
/** 题目:Lake Counting** 地址:http://poj.org/problem?id=2386** 题意:求w相连集合的个数**/ #include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std; struct node{    int x,y;}s,e; queue <node> q; int d[8][2] = {-1,0, 0,1, 1,1, 1,0, 0,-1 , -1,1, 1,-1, -1,-1}; //搜索到A点时,用来向A点周围的点延生 bool f[110][110]; // 标记char map[110][110];int n,m;  bool is_ok(int x, int y)         {    if(x>=1 && x<=n && y>=1 && y<=m &&  map[x][y] == 'W') return true;    return false;}  void  bfs(int x,int y){    f[x][y] = true;     //  标记此点已经访问过      s.x = x;    s.y = y ;    q.push(s);          //  第一个点入队     while(!q.empty())    {        s = q.front();         q.pop();        for(int i=0;i<8;i++)        {            int xx = s.x + d[i][0];             int yy = s.y + d[i][1];            while(is_ok(xx,yy))            {                 if(f[xx][yy]==false)                  {                     f[xx][yy] = true;                     e.x = xx;                    e.y = yy;                         q.push(e);                        }                xx += d[i][0];                yy += d[i][1];            }        }    }} int main(){    int i,j;    int ans;    while(scanf("%d %d",&n,&m)!=EOF)    {        ans=0;        memset(f,false ,sizeof(f));        while(!q.empty()) q.pop();        memset(map,0,sizeof(map));         for(i=1;i<=n;i++)            for(j=1;j<=m;j++)                scanf(" %c",&map[i][j]);         for(i=1;i<=n;i++)        {  // 从起点开始,遍历所有没有被标记的'W',然后用BFS把与此点相连的点全部标记。            for(j=1;j<=m;j++)                if(map[i][j]=='W'&&f[i][j]==false){                    bfs(i,j);                    ans++;}        }         printf("%d\n",ans);     }    return 0; }


0 0
原创粉丝点击