【Codeforce1182B】DFS

dfs找到中心点,利用坐标规律。

char p[510][510];
char p_cy[510][510];
map<int,int> x_map,y_map;
int max_x=0,max_y=0;
int cen_x=0,cen_y=0;
int total=0,cnt=0;
int r,c;
void dfs(int r0,int c0){
	if(r0<0||r0>=r||c0<0||c0>=c||p[r0][c0]=='.')	return ;
	p[r0][c0]='.';
	cnt++;
	x_map[c0]++;
	y_map[r0]++;
	
	if(max_x<x_map[c0]){
		max_x=x_map[c0];
		cen_x = c0;
	}
	if(max_y<y_map[r0]){
		max_y=y_map[r0];
		cen_y = r0;
	}
	dfs(r0-1,c0);
	dfs(r0+1,c0);
	dfs(r0,c0-1);
	dfs(r0,c0+1);
}
int main(){
	cin>>r>>c;
	int a=0,b=0;
	for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
			cin>>p[i][j];
			p_cy[i][j] = p[i][j];
			if(p[i][j]=='*'){
					total++;
					a=i;
					b=j;
			}
		}
	}
	if(total==0||total<5){
		cout<<"NO";
		return 0;
	}
	dfs(a,b);
	//cout<<cen_y<<" "<<cen_x<<endl;
	if(cnt!=total||cen_y<0||cen_y>=r||cen_x<0||cen_x>=c||max_x+max_y-1!=total||p_cy[cen_y-1][cen_x]=='.'||p_cy[cen_y+1][cen_x]=='.'||p_cy[cen_y][cen_x+1]=='.'||p_cy[cen_y][cen_x-1]=='.'){
		cout<<"NO";
		return 0;
	}
	cout<<"YES";
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Csdn_jey/article/details/91838085
今日推荐