BNUOJ 1038 Flowers (BFS), bnuojbfs

Source: Internet
Author: User

BNUOJ 1038 Flowers (BFS), bnuojbfs
FlowersTime Limit: 1000 msMemory Limit: 65535KB64-bit integer IO format: % lld Java class name: MainPrev Submit Status Statistics Discuss Next spring is approaching, and the directors of the General University are busy again.

There is an open space on the Beijing division Square. The boundary is surrounded by a polygon and the interior is divided into a grid. Gardeners want to plant some flowers in each grid of the polygon.

Please help us calculate the maximum amount of flowers you can plant.

The square is represented by an M * N character array ,". "and" * "represent a square, where" * "represents the boundary of the open space ,". "It is a space. Only spaces inside the boundary can be used for flower planting.
A space is inside the boundary. If it is set from this point and can only be moved up, down, left, or right, the boundary is eventually reached.

For example, the following is a 6*7 square.


The flower planting scheme is as follows (where numbers represent)
.. * 12 *.
.. * 34 *.
The first line of Input data is M and N (M and N cannot exceed 100), representing the size of a square.
The next step is a M * N character matrix, which indicates that the Output of the square corresponds to the Input data and outputs an integer, indicating the number of flowers that can be planted in the Input scenario. Sample Input

Sample Input16 7.........***....*..*...*..*....**.........Sample Input25 7.........***....*.*....***.........
Sample Output
Sample Output14Sample Output21
Source, Fourth Session of the Seventh Beijing Normal University Program Design Competition
#include<stdio.h>#include<queue>#include<string.h>using namespace std;const int N = 105 ;struct node{    int x,y;};char mapt[N][N];int vist[N][N],n,m;int dir[4][2]={0,1,0,-1,1,0,-1,0};int bfs(int x,int y){    queue<node>q;    node now,pre;    int ans=1,flag=1;    vist[x][y]=1;    now.x=x; now.y=y;    q.push(now);    while(!q.empty())    {        pre=q.front(); q.pop();        for(int e=0; e<4; e++)        {            now.x=pre.x+dir[e][0];            now.y=pre.y+dir[e][1];            if(now.x<0||now.x>=n||now.y<0||now.y>=m)            {                flag=0; continue;            }            if(!vist[now.x][now.y]&&mapt[now.x][now.y]=='.')            {                vist[now.x][now.y]=1;                ans++;                q.push(now);            }        }    }    if(flag==0) ans=0;    return ans;}int main(){    //int n,m;    while(scanf("%d%d",&n,&m)>0)    {        for(int i=0; i<n; i++)            scanf("%s",mapt[i]);        int ans=0;        memset(vist,0,sizeof(vist));        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)            if(!vist[i][j]&&mapt[i][j]=='.')             ans+=bfs(i,j);        printf("%d\n",ans);    }}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.