You have a given picture with size w×h. Determine if the given picture has a single “+” shape or not. A “+” shape is described below:
Find out if the given picture has single “+” shape.
给你一串字符,让你判断所有的*是否可以构成唯一的+号
我们可以先找到构成加号中间的星号,然后分别向四个方向dfs,dfs的过程中统计星号的个数,最后判断一下dfs星号的个数是否与所有星号的个数相同即可。
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=2*1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int n,m;
char g[505][505];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
bool st[505][505];
int deg=0;
void dfs(int x,int y,int dir){
//cout<<x<<' '<<y<<endl;
if(g[x][y]=='.') return ;
if(x<0 || y<0 || x>=n || y>=m) return ;
if(!st[x][y]) deg++;
st[x][y]=true;
dfs(x+dx[dir],y+dy[dir],dir);
}
void solve(){
cin>>n>>m;
for(int i=0;i<n;i++) cin>>g[i];
int cnt=0,sum=0,sum_copy=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]=='*') sum++;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i>=1 && j>=1 && i+1<n && j+1<m){
if(g[i][j]=='*' && g[i-1][j]=='*' && g[i+1][j]=='*' && g[i][j+1]=='*' && g[i][j-1]=='*'){
dfs(i,j,0);
dfs(i,j,1);
dfs(i,j,2);
dfs(i,j,3);
cnt++;
}
}
}
}
//cout<<cnt<<' '<<deg<<endl;
if(cnt!=1 || deg!=sum) cout<<"No"<<endl;
else cout<<"YES"<<endl;
}
int main(){
IOS;
solve();
return 0;
}