BNUOJ 1038 Flowers (BFS)
FlowersTime Limit: 1000 ms Memory Limit: 65535KB 64-bit integer IO format: % lld Java class name: Main Prev Submit Status Statistics Discuss Next spring is coming, the gardeners of Normal University started to get 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 array of M * N characters. And * represent a square. * 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
#include
#include
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
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