Vjudge链接:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19423
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

Squares
Squares |
A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines. For example, in the figure shown below, there are 3 squares-2 of size 1 and 1 of size 2. (The ``size" of a square is the number of lines segments required to form a side.)

Your problem is to write a program that automates the process of counting all the possible squares.
Input
The input file represents a series of game boards. Each board consists of a description of a square array of n2 dots (where 2 <= n <= 9) and some interconnecting horizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted as follows:
Line 1: n the number of dots in a single row or column of the arrayLine 2: m the number of interconnecting lines
Each of the next m lines are of one of two types:
H i j indicates a horizontal line in row i which connects
the dot in column j to the one to its right in column j + 1
or
V i j indicates a vertical line in column i which connects
the dot in row j to the one below in row j + 1
Information for each line begins in column 1. The end of input is indicated by end-of-file. The first record of the sample input below represents the board of the square above.
Output
For each record, label the corresponding output with ``Problem #1", ``Problem #2", and so forth. Output for a record consists of the number of squares of each size on the board, from the smallest to the largest. lf no squares of any size exist, your program should print an appropriate message indicating so. Separate output for successive input records by a line of asterisks between two blank lines, like in the sample below.
Sample Input
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
Sample Output
Problem #1 2 square (s) of size 1 1 square (s) of size 2 ********************************** Problem #2 No completed squares can be found.
Source
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 7. (Computational) Geometry :: Geometry Basics :: Rectangles
Root :: ACM-ICPC World Finals :: 1990 - Washington
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: More Advanced Topics :: Problem Decomposition :: Two Components - Complete Search and Geometry
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 4. Functions and Recursion :: Exercises
题意:n个点组成的正方体,其中有m条边,H i j表示边(i,j)~(i,j+1),V j i表示边(i,j)~(i+1,j),问不同边长的子正方体各有多少个。
注意:
①V的ij是反的。
②输出的格式。
代码:
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
using namespace std;
int test=0,n,m,h[10][10],v[10][10],a,b,sq[10];
char r;
int jude(int x,int zsi,int zsj)
{
for(int i=zsi;i<zsi+x;i++)
{
if(v[i][zsj]==0||v[i][zsj+x]==0)
{
return 0;
}
}
for(int i=zsj;i<zsj+x;i++)
{
if(h[zsi][i]==0||h[zsi+x][i]==0)
{
return 0;
}
}
return 1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(h,0,sizeof(h));
memset(v,0,sizeof(v));
memset(sq,0,sizeof(sq));
for(int i=1;i<=m;i++)
{
cin>>r>>a>>b;
if(r=='H')
{
h[a][b]=1;
}
else
{
v[b][a]=1;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=n-i;j++)
{
for(int w=1;w<=n-i;w++)
{
if(jude(i,j,w))
{
sq[i]++;
}
}
}
}
if(test!=0)
{
printf("\n**********************************\n\n");
}
printf("Problem #%d\n\n",++test);
int flag=0;
for(int i=1;i<n;i++)
{
if(sq[i]!=0)
{
flag=1;
printf("%d square (s) of size %d\n",sq[i],i);
}
}
if(flag==0)
printf("No completed squares can be found.\n");
}
return 0;
}