本题有以下注意点:
1.这一题最初按着书上的一问来理解的,但是对着原题的示例手动计算怎么都不对,重新读了原文,发现翻译有误差。
书上说“H i j和V i j分别代表边(i,j)-(i,j+1)和(i,j)-(i+1,j)”;
但是原文却是:H i j indicates a horizontal line in row i which connectsthe dot in column j to the one to its right in column j + 1
V i j indicates a vertical line in column i which connectsthe dot in row j to the one below in row j + 1
但是最终出于习惯,我全部吧"V"的两个坐标分别交换了。
2.最初写完,运行之后,输入第一行横向坐标之后,程序就卡住不动了,后来单步调试,发现是因为输入最初的两个数字之后,换行符被记录到原本打算存放H或V的位置了,后来做了相应的修改。
3,.本题在最后调试的过程中,发现单租数据结果是正确的,第二组就不对的,后来增加了结构体的初始化。
4.UVa的有些题真的好无聊,输出结果时,有些位置空一行,有些位置空两行。因为这细枝末节的原因,无端增加了许多PE错误。
#include<stdio.h>
#include<string.h>
struct point
{
int h ;
int v ;
}xian[10][10];
int jisuan(int chang,int hen,int shu)
{
int x1 =0,x2 =0,y1 =0,y2 =0;
for(int j =0;j<chang;j++)
{
x1 += xian[hen][shu+j].h;
x2 += xian[hen+chang][shu+j].h;
y1 += xian[hen+j][shu].v;
y2 += xian[hen+j][shu+chang].v;
}
if(x1+x2+y1+y2 == 4*chang)
return 1;
else
return 0;
}
int main()
{
int dian,n,kase =1;
char direction;
int a,b;
while(scanf("%d%d",&dian,&n) == 2)
{
if(kase != 1)
printf("\n**********************************\n\n");
for(int p =1;p<=dian;p++)
for(int q =1;q<=dian;q++)
{
xian[p][q].h = 0;
xian[p][q].v = 0;
}
while(n--)//输入全部数据
{
getchar();
scanf("%c",&direction);
if(direction == 'H')
{
scanf("%d%d",&a,&b);
xian[a][b].h = 1;
}
else
{
scanf("%d%d",&a,&b);
xian[b][a].v = 1;
}
}
printf("Problem #%d\n\n",kase++);
int k ,i,p,q,ji = 0;
for( i =1;i<dian;i++)//边长增加
{
k = 0;
for( p =1;p<=dian;p++)
for( q =1;q<=dian;q++)
{
if((p+i)>dian || (q+i)>dian)
break;
k += jisuan(i,p,q);
}
if(k != 0)
{
ji =1;
printf("%d square (s) of size %d\n",k,i);
}
}
if(ji == 0)
printf("No completed squares can be found.\n");
}
return 0;
}