边用Hij 和 Vij 表示,分别代表(i,j)-(i,j+1)和(i,j)-(i+1,j)
原题中的样例:
SampleInput
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
SampleOutput
Problem #1
2 square (s) of size 1
1 square (s) of size 2
Problem #2
No completed squares can be found.
代码:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int H[10][10];
int V[10][10];
int main()
{
int n,m,x,y,T = 0;
char c;
while (~scanf("%d%d",&n,&m)) {
getchar();
for (int i = 1 ; i <= n ; ++ i)
for (int j = 1 ; j <= n ; ++ j)
V[i][j] = H[i][j] = 0; //对V和H数组进行初始化
for (int i = 1 ; i <= m ; ++ i) {
scanf("%c%d%d",&c,&x,&y);
getchar();
if (c == 'H')
H[x][y] = 1;
else
V[y][x] = 1;
}
if (T ++) printf("\n**********************************\n\n");
printf("Problem #%d\n\n",T);
int sum = 0;
for (int l = 1 ; l <= n ; ++ l) {
int count = 0,flag = 0;
for (int i = 1 ; i+l <= n ; ++ i)
for (int j = 1 ; j+l <= n ; ++ j) {
flag = 1;
for (int h = j ; h < j+l ; ++ h)
if (!H[i][h] || !H[i+l][h]) flag = 0;
for (int v = i ; v < i+l ; ++ v)
if (!V[v][j] || !V[v][j+l]) flag = 0;
count += flag;
}
sum += count;
if (count) printf("%d square (s) of size %d\n",count,l);
}
if (!sum) printf("No completed squares can be found.\n");
}
return 0;
}