判断图形是否封闭算法

算法大意:如果一个图像是闭合的,那么对于其中的每一个结点来说,它至少有一个相连的出节点,也至少有一个入节点,加上自身,以它为中心的九宫格内必须至少有三个结点。因此,只需判断节点数是否都大于2即可,如果有一个结点不大于2,那么就是NO。

题意如下:

Description

Boyce have a pen, Boyce has a blank paper. Now, Boyce just uses this pen draw some curves. Please determine if these curves is closed.

Input

There are TT test cases. Include a 25×2525×25 dot matrix figure in each case, It only consists of '.' and '*' characters where the '.' character stand of white space.

Output

Just output 'Yes' or 'No' that stand of these curves is closed or not. Boyce ensures that the answer is unambiguous.

Sample Input 1

Note: For simplicity, sample input is a 5×55×5 dot matrix figure, Does not represent real input.

Copy
1
.....
.***.
.*.*.
.*.*.
.***.
Sample Output 1
Copy
Yes

Sample Input 2

Note: For simplicity, sample input is a 5×55×5 dot matrix figure, Does not represent real input.

Copy
1
.....
.***.
.*...
.***.
.***.
Sample Output 2
Copy
No

Sample Input 3

Note: For simplicity, sample input is a 5×55×5 dot matrix figure, Does not represent real input.

Copy
1
.....
...*.
..**.
.*.*.
****.
Sample Output 3
Copy
Yes

代码如下:

 #include<stdio.h>
bool HasEdge(char ch[25][25],int x,int y,int n);
int main()
{
char ch[25][25];
int num,n;
int i,j,k;
scanf("%d",&num);
n=25;
getchar();
for(k=1;k<=num;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%c",&ch[i][j]);
}
getchar();
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(ch[i][j]=='*')
{
if(HasEdge(ch,i,j,n)==false)
{
printf("No\n");
goto mybreake;
}
}
}
}
printf("Yes\n");
mybreake: continue;

}
bool HasEdge(char ch[25][25],int x,int y,int n)
{
int i,j,k=0;
for(i=-1;i<=1;i++)
{
if(x+i<n&&x+i>=0)
{
for(j=-1;j<=1;j++)
{
if(y+j<n&&y+j>=0)
{
if(ch[x+i][y+j]=='*')
{
k++;
}
}
}
}
}
if(k>2)
return true;
else
return false;
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值