寄王俊谦

鸡你太美  •  3个月前


include

using namespace std; int n, m; bool map[305][305]; bool vis[305][305];

void dfs(int x, int y) {

if (map[x][y] == true && vis[x][y] == false) {
	vis[x][y] = true;
	if (x + 1 <= m)
		dfs(x + 1, y);
	if (x - 1 >= 1)
		dfs(x - 1, y);
	if (y + 1 <= n)
		dfs(x, y + 1);
	if (y - 1 >= 1)
		dfs(x, y - 1);
	map[x][y] = false;

}

}

int main() {

int x, y;
cin >>  m >>  n;
int v;
for (int i = 1; i <= m; i++)
	for (int j = 1; j <= n; j++)
cin >>map[i][j];
		for (int i = 1; i <= m; i++) {

			dfs(i, 1);
			dfs(i, n);
		}
for (int j = 1; j <= n; j++) {

	dfs(1, j);
	dfs(m, j);
}
int ans = 0;
for (int i = 1; i <= m; i++) {

	for (int j = 1; j <= n; j++) {

		
		if (map[i][j] == true)
			ans++;
	}
}
printf("%d", ans);
return 0;

}


评论:

请先登录,才能进行评论