P1596 [USACO10OCT] Lake Counting S

快快好起来Aaaaaa!

能想到是bfs,但定势思维,不知道起点如何选了,其实很eaasy~

本想不弄字符数组。但那样只用数字标记会乱。还是这样好些

最致命的错误就是细节,比如等于是==,还有队列取队头是front(),不是top()

#include<bits/stdc++.h>
using namespace std;
char land[105][105];
int mark[105][105]={0};
struct point{
	int x,y;
};
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={1,0,-1,1,-1,1,0,-1};
int n,m,ans=0;
queue<point> q;
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>land[i][j];
		}
	}
	
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
//			找到起点
			if(land[i][j]=='W'&&mark[i][j]==0){
				q.push({i,j});
				mark[i][j]=1;
				ans++;
				while(!q.empty()){
					point temp=q.front();
					q.pop();
					for(int k=0;k<8;k++){
					int x=temp.x+dx[k];
					int y=temp.y+dy[k];	
//					越界判断
					if(x<0||y<0||x>n||y>m) continue;	 
					if(land[x][y]=='W'&&mark[x][y]==0) {
					q.push({x,y});
					mark[x][y]=1;	
					}
					}
					
	}
} 
		}
	}
	cout<<ans;
}

posted @ 2025-04-08 16:44  夕瑶^  阅读(8)  评论(0)    收藏  举报