80pts求调
查看原帖
80pts求调
1047798
nahida_badminton楼主2024/12/22 11:42
#include <bits/stdc++.h>
//说句闲话,研究珂学的最好方式是
//A了这道题,祝你们成功[滑稽]
using namespace std;
char ch;
int r, c, nx, ny, ax[] = {1, -1, 0, 0}, ay[] = {0, 0, 1, -1}, ans = 0;
bool mp[55][55];

void dfs(int x, int y){
	mp[x][y] = 0;
	for (int i = 0; i < 4; i++){
		nx = x + ax[i], ny = y + ay[i];
		if (!mp[nx][ny] || nx <= -1 || ny <= -1 || nx >= r || ny >= c) continue;
		dfs(nx, ny);
	}
}

int main(){
	scanf("%d %d", &r, &c);
	for (int i = 0; i < r; i++){
		for (int j = 0; j < c; j++){
			cin >> ch;
			mp[i][j] = ch - '0';
		}
	}
	for (int i = 0; i < r; i++){
		for (int j = 0; j < c; j++){
			if (mp[i][j]){
				dfs(i, j);
				ans++;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}

2024/12/22 11:42
加载中...