#define LOCAL0
#include <stdlib.h>
#include <stdio.h>
#include "iostream"
#include "sstream"
#include <string.h>
#include <cmath>
#define inf 0x3f3f3f3f
using namespace std;
//mp[i][j]存的是从第i个点到第j个点有没有路。1<=i,j<=n*n;
int mp[200][200];
int valid(int mlen, int pos, int len)
{
int j=(pos-1)%mlen+1, i=(pos-1)/mlen+1;
if(i+len>mlen||j+len>mlen) return 0;
int tmp,x,y;
for( tmp=len,x=pos,y=pos+mlen*len;
tmp&&mp[x][x+1]&&mp[y][y+1];
--tmp,++x,++y )
;
if(tmp) return 0;
for( tmp=len,y=x,x=pos;
tmp&&mp[x][x+mlen]&&mp[y][y+mlen];
--tmp,x+=mlen,y+=mlen )
;
if(tmp) return 0;
return 1;
}
int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int pset=0;
int n,m,i,j,sz;
char ch;
while(cin>>n&&n){
cin>>m;
memset(mp,0,sizeof(mp));
while(m--){
cin>>ch>>i>>j;
if(ch=='H'){
int pos=n*(i-1)+j;
mp[pos][pos+1]=1;
}
else if(ch=='V'){
int pos=n*(j-1)+i;
mp[pos][pos+n]=1;
}
}
int arr[10]={0};
for(sz=1;sz<n;++sz){
for(i=1;i<=n*n;++i){
if(valid(n,i,sz)){
arr[sz]++;
}
}
}
if(pset>0){
printf("\n**********************************\n\n");
}
printf("Problem #%d\n", ++pset);
int flag=0;
printf("\n");
for(i=1;i<10;i++){
if(arr[i]){
printf("%d square (s) of size %d\n", arr[i],i);
flag=1;
}
}
if(!flag)
printf("No completed squares can be found.\n");
}
return 0;
}
这是测试数据:
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1