原题对树边的定义和所给的样例不符,交换读到的竖边起始顶点的横纵坐标就行了
#include <stdio.h>
typedef struct Table
{
int H;
int V;
}Cell;
void Count(Cell Table[10][10], int *Size, int n, int size)
{//传二维数组,注明列数
//对顶点集Table遍历
for(int i = 1; i <= n - size; i++)
{
for(int j = 1; j <= n - size; j++)
{
int flag = 1;
//if(Table[i][j].H && Table[i][j].V && Table[i+size][j+size-1].H && Table[i+size-1][j+size-2].V)
for(int k = i; k < i+size; k++ )
{
if(!Table[k][j].V) {flag = 0; break;} //左边
if(!Table[k][j+size].V) {flag = 0; break;} //右边
}
if(!flag) continue;
for(int k = j; k < j + size; k++)
{
if(!Table[i][k].H) {flag = 0; break;} //上边
if(!Table[i+size][k].H) {flag = 0; break;} //下边
}
if(flag) Size[size]++;
}
}
}
int main()
{
int n, t, num = 0;
while(scanf("%d%d", &n, &t) == 2)
{
int Size[9] = {0},x, y;
char ch;
Cell Table[10][10];
for(int i = 1; i <= 9; i++)
{
for(int j = 1; j <= 9; j++)
{
Table[i][j].H = 0;
Table[i][j].V = 0;
}
}
getchar();
while(t--)
{
scanf("%c%d%d", &ch, &x, &y);
if(ch == 'H') Table[x][y].H = 1;
else if(ch == 'V') Table[y][x].V = 1;
getchar();
}
for(int size = 1; size < n; size++)
{
Count(Table, Size, n, size);
}
if(num) printf("\n**********************************\n\n");
printf("Problem #%d\n\n", ++num);
int flag = 1;
for(int i = 1; i < n; i++)
{
if(Size[i]) {printf("%d square (s) of size %d\n", Size[i], i); flag = 0;}
}
if(flag) printf("No completed squares can be found.\n");
}
return 0;
}