您的位置:首页 > 其它

UVa 201 Squares

2018-01-21 15:24 423 查看
题意:给出nXn的矩阵点,给出m条线段,判断有多少个多大的正方形

注:1.H i j表示第i行第j个点和j+1个点之间有连线

        2.V i j表示第i列,第j行和j+1行有连线

易错点:1.思路上:分别标记竖线与横线,枚举边长,对称判断。

              比如:先判断竖线->v[x][y]与v[x][y+k],在判断横线->h[x][y]与h[x+k][y]

            2.判断范围时注意4X4的最多边长为3,i,j只能枚举到3,否则发生越界!

代码:#include <stdio.h>
#include<string.h>
int h[11][11],v[11][11];//分别标记横线与竖线
int main()
{
int n,m,tot=0,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot++;
memset(h,0,sizeof(h));
memset(v,0,sizeof(v));
int i,j,x,y;
char ch;
for(i=1; i<=m; i++)
{
getchar();
scanf("%c %d %d",&ch,&x,&y);
if(ch=='H')
h[x][y]=1;
else if(ch=='V')
v[y][x]=1;//注意调换xy位置
}
if(tot>1)
{
printf("\n**********************************\n");
printf("\n");
printf("Problem #%d\n",tot);
printf("\n");
}
else
{
printf("Problem #%d\n",tot);
printf("\n");
}
int flag=0;//是否有正方形
for(k=1; k<n; k++)//枚举边长
{
int count=0;
for(i=1; i<=n-k; i++)//x+k和y+k等于缩小了范围
{
for(j=1; j<=n-k; j++)
{
int mark=1;这个点上有没有正方形
int x,y;
for(x=i,y=j; x<i+k&&mark; x++)
{
if(!v[x][y+k]||!v[x][y])//对称位置上都有线段才行
mark=0;
}
for(x=i,y=j; y<j+k&&mark; y++)
{
if(!h[x][y]||!h[x+k][y])
mark=0;
}
if(mark)
count++;
}
}
if(count)
{
printf("%d square (s) of size %d\n",count,k);
flag=1;
}
}
if(flag==0)
printf("No completed squares can be found.\n");

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: