CF1722E L-shapes

Problem Link

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:

  1. 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.
  2. 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值