A simple implementation problem
The core step of the problem is to check whether each L-shape fits the standard, and is not in contact with any other L-shape. This can be easily solved by independently checking each individual cell.
Firstly, for a shaded shell, the number of adjacent shade cells (either sharing an edge or a corner) must be equal to two, or else it will not form an L-shape
Then, for each shade cell, there are two scenarios:
- The cell is the ceter node of the L-shape (i.e. the cell in the middle who shares an edge with both of the two other cells)
In this case, the two common edges of the center node must be adjacent, or else the three of them will form a straight line. - The cell is the marginal node of the L-shape
In this case, one of the adjacent cell must share an edge with our discussed node, and the other one must share a corner. Furthermore, these two other cells must also share an edge with each other
With these conclusions, all we have to do is to implement this checking procedure to each cell.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int Maxn=55;
const int Maxm=5;
const int dx[]={-1,0,1,0,-1,1,1,-1};
const int dy[]={0,1,0,-1,1,1,-1,-1};
char s[Maxn][Maxn];
bool a[Maxn][Maxn];
int n,m;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
int main()
{
// freopen("in.txt","r",stdin);
int T=read();
while(T--)
{
n=read(),m=read();
for(int i=1;i<=n;++i)
{
scanf("%s",s[i]+1);
for(int j=1;j<=m;++j)
a[i][j]=(s[i][j]=='*');
}
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
if(!a[i][j])continue;
int tot=0,cnt=0;
for(int k=0;k<8;++k)
{
int x=i+dx[k],y=j+dy[k];
if(x<1 || x>n || y<1 || y>m || !a[x][y])continue;
if(k<4)++tot;
else ++cnt;
}
if(tot+cnt!=2){puts("NO");goto GG;}
for(int k=0;k<4;++k)
{
int x=i+dx[k],y=j+dy[k];
int u=i+dx[(k+1)%4],v=j+dy[(k+1)%4];
if(x<1 || x>n || y<1 || y>m || !a[x][y])continue;
if(u<1 || u>n || v<1 || v>m || !a[u][v])continue;
goto EOL;
}
for(int k=0;k<4;++k)
{
int x=i+dx[k],y=j+dy[k];
int u=i+dx[k+4],v=j+dy[k+4];
if(x<1 || x>n || y<1 || y>m || !a[x][y])continue;
if(u<1 || u>n || v<1 || v>m || !a[u][v])continue;
goto EOL;
}
for(int k=0;k<4;++k)
{
int x=i+dx[k],y=j+dy[k];
int u=i+dx[(k+3)%4+4],v=j+dy[(k+3)%4+4];
if(x<1 || x>n || y<1 || y>m || !a[x][y])continue;
if(u<1 || u>n || v<1 || v>m || !a[u][v])continue;
goto EOL;
}
puts("NO");
goto GG;
EOL:;
}
puts("YES");
GG:;
}
return 0;
}