全部不过 只有样例过了
查看原帖
全部不过 只有样例过了
866969
telankesi楼主2023/7/12 15:34
#define _CRT_SECURE_NO_WARNINGS 1
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<ctime>
#include <cstdlib>
#define MAXN 100008
using namespace std;


int n, m;
char map[1001][1001];
int step[4][2] = { 0,1,0,-1,1,0,-1,0 };
int vis[1001][1001];
int ans = 0;
int out(int x,int y) {
	if (x<1 || x>n || y<1 || y>m)
		return 1;
	return 0;
}
void dfs(int x,int y,int& num,int &flag) {
	int xx, yy;
	for (int i = 0; i < 4; i++) {
		xx = x + step[i][0];
		yy = y + step[i][1];
		if (out(xx, yy)) {
			flag = 1;
			continue;
		}
		if (vis[xx][yy] == 0&&map[xx][yy]=='0') {
				vis[xx][yy] = 1;
				num++;
				dfs(xx, yy,num,flag);
			}
		
	}


}
int main()
{   
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf(" %c", &map[i][j]);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (!vis[i][j] && map[i][j] == '0') {
				int num = 1;
				int flag = 0;
				dfs(i, j,num,flag);
				if (flag == 0)
					ans += num;
			}
		}
	}
	cout << ans;
	return 0;
}
2023/7/12 15:34
加载中...