G. Sheba's Amoebas -ICPC North Central NA Contest 2017

https://nanti.jisuanke.com/t/43374

题意

  判断像素矩阵中像素围成的封闭区域的个数

思路

  对于每一个像素,检查周围是否有像素形成相连

  从当前下一个未标记的像素开始进行搜索,搜索结果作为下一个节点进行搜索,在搜索的同时进行染色

  遇到已经染色的像素,判断为一个封闭空间

代码

  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;


bool compare(int a, int b)
{
	return a < b;//升序
}

int filed[102][102] = {0};
bool Fill_Color(int x , int y)
{
	bool flag = 0;
	if(filed[x+1][y] == 1){
		filed[x+1][y] = 2;
		Fill_Color(x+1 , y);
		flag = 1;
	}
	if(filed[x-1][y] == 1){
		filed[x-1][y] = 2;
		Fill_Color(x-1 , y);
		flag = 1;
	}
	if(filed[x][y+1] == 1){
		filed[x][y+1] = 2;
		Fill_Color(x , y+1);
		flag = 1;
	}
	if(filed[x][y-1] == 1){
		filed[x][y-1] = 2;
		Fill_Color(x , y-1);
		flag = 1;
	}
	if(filed[x+1][y+1] == 1){
		filed[x+1][y+1] = 2;
		Fill_Color(x+1 , y+1);
		flag = 1;
	}
	if(filed[x-1][y-1] == 1){
		filed[x-1][y-1] = 2;
		Fill_Color(x-1 , y-1);
		flag = 1;
	}
	if(filed[x+1][y-1] == 1){
		filed[x+1][y-1] = 2;
		Fill_Color(x+1 , y-1);
		flag = 1;
	}
	if(filed[x-1][y+1] == 1){
		filed[x-1][y+1] = 2;
		Fill_Color(x-1 , y+1);
		flag = 1;
	}
	return flag;
}
char pool[100] = {0};
int main()
{
	int a , b;
	cin >>a >>b;
	int counter = 0;
	for(int i = 1;i<=a;i++)
	{
		cin >>pool;
		for(int j = 0;j<=b;j++)
		{
			if(pool[j] == '#')
			{
				filed[i][j] = 1;
			}
		}
	}
	for(int i = 1;i<=a;i++)
	{
		for(int j = 0;j<=b;j++)
		{
			if(filed[i][j] == 1)
			{
				Fill_Color(i , j);
				counter++;
			}
		}
	}
	cout<<counter;
}

  

作者:YukiRinLL

出处:YukiRinLL的博客--https://www.cnblogs.com/SutsuharaYuki/

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但请保留该声明。

原文地址:https://www.cnblogs.com/SutsuharaYuki/p/12443570.html