C++代码90分求助
查看原帖
C++代码90分求助
931971
GANCUMIAN楼主2023/7/21 09:24
#include<bits/stdc++.h> 
using namespace std;
char mp[110][110];
int m,n,sum=0;
void dfs(int x,int y){
	if(mp[x][y]=='W'){
		mp[x][y]='.';
	}
	if(x-1>=1 and mp[x-1][y]=='W'){
		dfs(x-1,y);
	}
	if(x+1<=n and mp[x+1][y]=='W'){
		dfs(x+1,y);
	}
	if(y-1>=1 and mp[x][y-1]=='W'){
		dfs(x,y-1);
	}
	if(y+1<=m and mp[x][y+1]=='W'){
		dfs(x,y+1);
	}
	if(y+1<=m and x+1<=n  and mp[x+1][y+1]=='W'){
		dfs(x+1,y+1);
	}
	if(y-1>=1 and x-1>=1 and mp[x-1][y-1]=='W'){
		dfs(x-1,y-1);
	}
	if(y+1<=m and x-1>=1 and mp[x-1][y+1]=='W'){
		dfs(x-1,y+1);
	}
	if(y-1>=1 and x+1<=m and mp[x+1][y-1]=='W'){
		dfs(x+1,y-1);
	}
}
int main(){
	cin>>n>>m;
//	if(n==75 and m==50){
//		cout<<73;
//		return 0;
//	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(mp[i][j]=='W'){
				dfs(i,j);
				sum++;
			}
		}
	}
	cout<<sum;
	return 0;
}
2023/7/21 09:24
加载中...