1. 程式人生 > >codevs 1569 最佳綠草

codevs 1569 最佳綠草

空間限制 empty queue 黃金 get include def else one

時間限制: 1 s 空間限制: 128000 KB 題目等級 : 黃金 Gold
題目描述 Description


貝茜正計劃著這一天如何美美地咀嚼春天的綠草,遠望著農民約翰鐘愛的並被分
割為R (1 <= R <= 100) 行和 C (1 <= C <= 100) 列的草場。她想去數一數草場
有多少個草叢。

每個草叢在地圖上用‘#‘來表示,或者兩個‘#‘連在一起(但不是在一個對角線),
給出草場地圖,請告訴貝茜草場上一共有多少個草叢。

例如,下面有一張草場地圖 R=5, C=6:

.#....
..#...
..#..#
...##.
.#....

這個草場一共有5個草叢。(1,2);(2,3)+(3+3);(3,6);(4,4)+(4,5);(5,2)

輸入描述 Input Description


* 第 1 行: 2個用空格隔開的整數 R , C

* 第 2 至 R+1 行: 草場地圖信息

輸出描述 Output Description

* 草場上草叢的總個數。

樣例輸入 Sample Input

5 6
.#....
..#...
..#..#
...##.
.#....

樣例輸出 Sample Output

5

#include<iostream>
#include
<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<queue> using namespace std; const int N=101; const int xd[]={0,-1,0,1}; const int yd[]={-1,0,1,0}; struct node{ int x,y; }now,top,nxt; char a[N][N]; bool vis[N][N]; int answer;
int n,m; queue<node>q; inline int read() { int x=0;char c=getchar(); while(c<0||c>9)c=getchar(); return x=c-0; } inline void bfs(int x,int y) { answer++; now.x=x; now.y=y; q.push(now); vis[x][y]=1; while(!q.empty()) { top=q.front(); q.pop(); for(int i=0;i<4;i++) { int xx=xd[i]+top.x; int yy=yd[i]+top.y; if(a[xx][yy]&&xx>0&&xx<=n&&yy>0&&yy<=m) { a[xx][yy]=0; nxt.x=xx; nxt.y=yy; q.push(nxt); } } } } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char c; cin>>c; if(c==#)a[i][j]=1; else a[i][j]=0; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]) bfs(i,j); printf("%d",answer); return 0; }

codevs 1569 最佳綠草