跳到正文

个人记录

7.8-晚上河西教室1 -- 广搜3 洪水填充

zqrmon约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年7月9日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 10 分钟

版本

  1. v1当前

T628539 水塘个数

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int b1[8]{0,1,-1,0,1,-1,1,-1};
int b2[8]={1,0,0,-1,1,1,-1,-1};
char a[105][105];
struct f{
	int x,y;
}; 
queue<f>q;
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]=='W'){
				ans++;
				q.push({i,j});
				while(q.size()>0){
						f f1=q.front();
						q.pop();
					for(int j=0;j<8;j++){
						if(f1.x+b1[j]>-1&&f1.x+b1[j]<n&&f1.y+b2[j]>-1&&f1.y+b2[j]<m&&a[f1.x+b1[j]][f1.y+b2[j]]=='W')
						{
							q.push({f1.x+b1[j],f1.y+b2[j]});
							a[f1.x+b1[j]][f1.y+b2[j]]='.';
						}
					}
				}
			}
		}
	}
	cout<<ans;
	return 0;
}

T628540 细胞个数

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int b1[4]{0,1,-1,0};
int b2[4]={1,0,0,-1};
char a[105][105];
struct f{
	int x,y;
}; 
queue<f>q;
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]>'0'){
				ans++;
				q.push({i,j});
				while(q.size()>0){
						f f1=q.front();
						q.pop();
					for(int j=0;j<4;j++){
						if(f1.x+b1[j]>-1&&f1.x+b1[j]<n&&f1.y+b2[j]>-1&&f1.y+b2[j]<m&&a[f1.x+b1[j]][f1.y+b2[j]]>'0')
						{
							q.push({f1.x+b1[j],f1.y+b2[j]});
							a[f1.x+b1[j]][f1.y+b2[j]]='0';
						}
					}
				}
			}
		}
	}
	cout<<ans;
	return 0;
}

T628537 全球变暖

C++
#include<bits/stdc++.h>
using namespace std;
int n,ans,ans1;
int fx[6]={-1,0,1,0};
int fy[6]={0,1,0,-1};
char d[1010][1010],f[1010][1010]; 
void dfs(int x,int y) 
{
	d[x][y] = '.';
	for(int i = 0;i < 4;i++) 
	{
		int xt = x + fx[i],yt = y + fy[i];
		if(d[xt][yt] != '.' && xt > 0 && xt <= n && yt > 0 && yt <= n) dfs(xt,yt);
	}
	return;
}
void df(int x,int y)
{
	f[x][y] = '.';
	for(int i = 0;i < 4;i++) 
	{
		int xt = x + fx[i],yt = y + fy[i];
		if(f[xt][yt] == '#' && xt > 0 && xt <= n && yt > 0 && yt <= n) df(xt,yt);
	}
	return;
}
int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			cin >> d[i][j],f[i][j] = d[i][j];
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++) 
			if(d[i][j] == '#' && (d[i-1][j] == '.' || d[i+1][j] == '.' || d[i][j-1] == '.' || d[i][j+1] == '.')) 
				d[i][j] = '-'; 
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			if(f[i][j] == '#')
			{
				ans1++;
				df(i,j);
			}
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			if(d[i][j] == '#')
			{
				ans++;
				dfs(i,j);
			}
	cout<<ans1 - ans;
	return 0;
}

T579122 Ovce

C++
#include <bits/stdc++.h>
using namespace std;
int n, m;
int b1[4] = {0, 1, -1, 0};
int b2[4] = {1, 0, 0, -1};
char a[255][255];
bool jl[255][255];
int qo, qv;
int o, v;
void dfs(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == '#') {
        return;
    }
    if (jl[x][y]) {
        return;
    }
    jl[x][y] = true;
    if (a[x][y] == 'o') {
        o++;
    }
    if (a[x][y] == 'v') {
        v++;
    }
    for (int i = 0; i < 4; i++) {
        dfs(x + b1[i], y + b2[i]);
    }
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
            jl[i][j] = false;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] != '#' && !jl[i][j]) {
                o = 0;
                v = 0;
                dfs(i, j);
                if (o > v) {
                    qo += o;
                } else {
                    qv += v;
                }
            }
        }
    }
    cout << qo << " " << qv;
    return 0;
}

D4181 流感传染

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
	int n, c = 0;
	cin >> n;
	int b[n+2][n+2]={0};
	char a[n + 2][n + 2] = {'#'};
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
			if(a[i][j]=='@')
			{
				b[i][j]++;
			}
		}
	}
	int m;
	cin >> m;
	for (int k = 2; k < m; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (a[i][j] == '@'&&b[i][j]>=1) {
					if (a[i + 1][j] == '.') {
						a[i + 1][j] = '@';
					}
					if (a[i][j + 1] == '.') {
						a[i][j + 1] = '@';
					}
					if (a[i][j - 1] == '.') {
						a[i][j - 1] = '@';
					}
					if (a[i - 1][j] == '.') {
						a[i - 1][j] = '@';
					}
					b[i][j]++;
				}
				else
				{
					b[i][j]++;
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(a[i][j]=='@')
			{
				c++;
			}
		}
	}
	cout << c;
	return 0;
}

D4331 【例8.2】细胞

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int b1[4]{0,1,-1,0};
int b2[4]={1,0,0,-1};
char a[105][105];
struct f{
	int x,y;
}; 
queue<f>q;
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(a[i][j]>'0'){
				ans++;
				q.push({i,j});
				while(q.size()>0){
						f f1=q.front();
						q.pop();
					for(int j=0;j<4;j++){
						if(f1.x+b1[j]>-1&&f1.x+b1[j]<n&&f1.y+b2[j]>-1&&f1.y+b2[j]<m&&a[f1.x+b1[j]][f1.y+b2[j]]>'0')
						{
							q.push({f1.x+b1[j],f1.y+b2[j]});
							a[f1.x+b1[j]][f1.y+b2[j]]='0';
						}
					}
				}
			}
		}
	}
	cout<<ans;
	return 0;
}
原文