poj 2386 bfs
来源:互联网 发布:macbookair软件下载 编辑:程序博客网 时间:2024/05/16 01:08
Description
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (‘.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
Input
Line 1: Two space-separated integers: N and M
Lines 2..N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.
OutputLine 1: The number of ponds in Farmer John’s field.
Sample Input
10 12
W……..WW.
.WWW…..WWW
….WW…WW.
………WW.
………W..
..W……W..
.W.W…..WW.
W.W.W…..W.
.W.W……W.
..W…….W.
Sample Output
3
Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
第一次尝试这种写法 orz
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set>using namespace std;#define ll long long#define PQ priority_queue#define MP make_pair#define PB push_back#define PF push_front#define POB pop_back#define POF pop_front#define PRF first#define PRS second#define fr(x) freopen(x,"r",stdin)#define fw(x) freopen(x,"w",stdout)#define cls(ar,val) memset ( ar, val, sizeof ( ar ) )#define debug(a) cerr<<#a<<"=="<<a<<endl#define lp(loop,start,end) for ( int loop = start; loop < end; ++loop )#define lpd(loop,start,end) for ( int loop = start; loop > end; --loop )#define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop )#define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop )#define qmax(a,b) (((a)>(b))?(a):(b))#define qmin(a,b) (((a)<(b))?(a):(b))#define qabs(a) (((a)>=0)?(a):(-(a)))const int INF = 0x3fffffff;const int SINF = 0x7fffffff;const long long LINF = 0x3fffffffffffffff;const long long SLINF = 0x7fffffffffffffff;const int maxn=100+10;int n,m;char mp[maxn][maxn];void print() { lp(i,0,n) { lp(j,0,m) { printf("%c",mp[i][j]); } printf("\n"); }}//int check(int x,int y) {// if(x<0||x>=n-1||y<0||y>m-1) return 0;// return 1;//}void dfs(int x,int y) { mp[x][y]='.'; for(int i=-1;i<=1;i++) { for(int j=-1;j<=1;j++) { int nx=x+i;int ny=y+j; if(x>=0&&x<n&&y>=0&&y<m&&mp[nx][ny]=='W') { dfs(nx,ny); } } }// print();// printf("*************\n"); return ;}void input () { // input method lp(i,0,n) { scanf("%s",mp[i]); }// print();}void work () { // main work int ans=0; lp(i,0,n) { lp(j,0,m) { if(mp[i][j]=='W') { dfs(i,j); ans++; } } } printf("%d\n",ans);}int main() {// fr("input.txt"); while(scanf("%d%d",&n,&m)!=EOF) { input(); work(); } return 0;}
- poj 2386 bfs
- poj 2386 BFS+枚举
- BFS练习-POJ.2386
- poj 2386 bfs
- poj 2386 Lake Counting (BFS~)
- POJ 2386 Lake Counting-------BFS
- 【BFS】-POJ-2386-Lake Counting
- Poj 2386 Lake Counting【Bfs】
- 【BFS】poj 2386 Lake Counting
- [BFS] poj
- poj 2386Lake Counting(DFS, BFS)
- poj 2386Lake Counting(BFS搞定)
- poj 2386 Lake Counting 解题报告(BFS)
- POJ 2386 Lake Counting (DFS or BFS)
- POJ 2386 Lake Counting(dfs or bfs)
- POJ 2386 Lake Counting(BFS)
- 【poj】2386 Lake Counting bfs||dfs
- poj 3626 简单bfs
- 学习简记
- 大一的自己
- [PAT乙级]1003. 我要通过!(20)
- 设置TextEdit 设置文本框只能输入限制的内容
- spark修炼之一wordcount
- poj 2386 bfs
- 中国证监会对方正科技立案调查的《行政处罚决定书》的具体内容
- GPU与CPU版本的矩阵乘法对比
- pat 1007. 素数对猜想 (20)
- numpy使用MKL库提升计算性能
- numpy性能优化
- numpy中的matrix矩阵处理
- Unity手游制作记-制作通用镜头控制器
- 神经网络入门