啊不能再摸鱼了,好好学习!
总而言之想到其高度对称性就不难的一道题目(然而还是摸了好久)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define MAXN 10
int main(void){
int m,n,h[MAXN][MAXN],v[MAXN][MAXN],num[MAXN],i,x,y,e1,e2,max,e,Y,N,cnt=1;
char ch;
while ((scanf("%d%d",&m,&n)==2)&&m&&n){
N=0;
memset(h,0,sizeof(h));
memset(v,0,sizeof(v));
memset(num,0,sizeof(num));
for (i=0;i<n;i++){
scanf("%c",&ch);
if (ch=='H'){scanf("%d%d",&x,&y);h[x][y]=1;}
else if (ch=='V'){scanf("%d%d",&x,&y);v[x][y]=1;}
else i--;
}
/* for (i=1;i<=m;i++){
for (e=1;e<=m;e++){
printf ("h[%d][%d]=%d v[%d][%d]=%d\n",i,e,h[i][e],i,e,v[i][e]);
}
}*/
for (i=1;i<m;i++)//边长小于n
for (e1=1;e1<m;e1++)//x轴开始检测,最后一边不用考虑
for (e2=1;e2<m;e2++){//第e2个点
Y=0;
max=e2>e1?e2:e1;//选取最小边减小工作量
if (max<=m-i){
Y=1;
for (e=0;e<i;e++)
if (!h[e1][e2+e]||!h[e1+i][e2+e]||!v[e2][e1+e]||!v[e2+i][e1+e]) Y=0;
}
if (Y){
//printf ("e1=%d e2=%d i=%d\n",e1,e2,i);
num[i]++;
N=1;
}
}
printf("\nProblem #%d\n\n",cnt++);
for (i=1;i<=m;i++){
if (num[i]) printf("%d square (s) of size %d\n",i,num[i]);
if (!N&&i==m) printf("No completed squares can be found.\n\n");
}
printf("**********************************\n");
}
return 0;
}