$P1596 [USACO10OCT]湖计数Lake Counting$

(problem)

其实这题吧(DFS)好写一点(大雾

所以就不讲(DFS)了 em

(BFS)的话 主要是 判重。 方向。 队列。(没了吧

至于位置 用两个队列?还是(pair)?还是结构体?

[ ext{结构体大法好啊。emm} ]

#ifdef Dubug

#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
	while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
	return res * f ;
}
int n , m ;
const int N = 100 + 5 ;//范围
char c[N][N] ;
bool vis[N][N] ;
struct node {
	int x , y ;
};
queue < node > q ;
int dx[] = {0,0,0,1,-1,1,1,-1,-1} ;
int dy[] = {0,1,-1,0,0,-1,1,-1,1} ;//8个方向。
int ans = 0 ;
inline void bfs(int i,int j) {//累加ans的值 因为一个水坑只会判断一次。
	ans ++ , q.push(node{i,j}) , vis[i][j] = true ;//第一个特殊处理。
	while(!q.empty()) {
		int x = q.front().x , y = q.front().y ; q.pop() ;//取出来一个。
		for(register int i=1;i<=8;i++){//不习惯 下标从 0 开始
			int cx = x + dx[i] , cy = y + dy[i] ;
			if(vis[cx][cy] or cx > n or cx < 1 or cy > m or cy < 1 or c[cx][cy] != 'W') continue ;//不符合条件就跳过。
			q.push(node{cx,cy}) , vis[cx][cy] = true ;//入队。
		}
	}
}
signed main() {
	memset(vis,0,sizeof(vis)) ;
	n = In() , m = In() ;
	for(register int i=1;i<=n;i++)
		for(register int j=1;j<=m;j++) cin >> c[i][j] ;
	for(register int i=1;i<=n;i++)
		for(register int j=1;j<=m;j++) if(!vis[i][j] and c[i][j] =='W') bfs(i,j) ;//bfs
	cout << ans << endl ;//输出答案
	return 0 ;
}
不存在十全十美的文章 如同不存在彻头彻尾的绝望
原文地址:https://www.cnblogs.com/qf-breeze/p/10589646.html