算法大意:如果一个图像是闭合的,那么对于其中的每一个结点来说,它至少有一个相连的出节点,也至少有一个入节点,加上自身,以它为中心的九宫格内必须至少有三个结点。因此,只需判断节点数是否都大于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.
1
.....
.***.
.*.*.
.*.*.
.***.
Sample Output 1
Yes
Sample Input 2
Note: For simplicity, sample input is a 5×55×5 dot matrix figure, Does not represent real input.
1
.....
.***.
.*...
.***.
.***.
Sample Output 2
No
Sample Input 3
Note: For simplicity, sample input is a 5×55×5 dot matrix figure, Does not represent real input.
1
.....
...*.
..**.
.*.*.
****.
Sample Output 3
Yes
代码如下:
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;
}