全WA求助
查看原帖
全WA求助
467775
2333?楼主2023/8/15 14:29
#include <bits/stdc++.h>
using namespace std;
#define W 105
struct node{
	int x,y;
	node(){}
	node(int ax,int by):x(ax),y(by){}
};
int a[W][W];
int n,m,cnt,ans,col[W*W],vst[W][W];
int fx[5]={0,1,-1,0,0};
int fy[5]={0,0,0,1,-1};
bool pd(int x,int y)
{
	return ((a[x][y+1]==a[x][y+2])&&(a[x][y]==a[x][y+1]))||((a[x][y-1]==a[x][y])&&(a[x][y]==a[x][y+1]))||((a[x][y-1]==a[x][y-2])&&(a[x][y]==a[x][y-1]))||((a[x-1][y]==a[x-2][y])&&(a[x-1][y]==a[x][y]))||((a[x-1][y]==a[x][y])&&(a[x][y]==a[x+1][y]))||((a[x+1][y]==a[x+2][y])&&(a[x+1][y]==a[x][y]));
}
void bfs(int sx,int sy)
{
	if((a[sx-1][sy]!=a[sx][sy]||a[sx+1][sy]!=a[sx][sy])&&(a[sx][sy-1]!=a[sx][sy]||a[sx][sy+1]!=a[sx][sy])) return;
	queue<node> q;
	q.push(node(sx,sy));
	vst[sx][sy]=1;
	col[++cnt]=1;
	
	while(q.size())
	{
		node nw=q.front();q.pop();
	//	cout<<nw.x<<" "<<nw.y<<endl;
		for
		(int i=1;i<=4;i++)
		{
			int nx=nw.x+fx[i],ny=nw.y+fy[i];
			if(a[nx][ny]!=a[nw.x][nw.y]||vst[nx][ny]||nx<1||ny<1||nx>n||ny>m) continue;
			if(!pd(nx,ny)) continue;
			vst[nx][ny]=1;
			col[cnt]++;
			q.push(node(nx,ny));
		}
		
	}
//	cout<<cnt<<":"<<a[sx][sy]<<" "<<col[cnt]<<endl;
}
int main()
{
	scanf("%d%d",&n,&m);
	memset(a,0x3f,sizeof(a));
	for(int i=1;i<=n;i++)
	{
		string s;
		cin>>s;
		for(int j=0;j<m;j++)
		a[i][j+1]=s[j];
	}

	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)	
			if(!vst[i][j]) bfs(i,j);
	for(int i=1;i<=cnt;i++)
		if(col[i]>=3) ans++;
	printf("%d",ans); 
	
}
2023/8/15 14:29
加载中...