A children’s board game consists of a square array of dots that contains lines connecting some of thepairs of adjacent dots. One part of the game requires that the players count the number of squares ofcertain 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 forma 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 arrayof n2 dots (where 2 ≤ n ≤ 9) and some interconnecting horizontal and vertical lines. A record for asingle 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 linesEach of the next m lines are of one of two types:H i j indicates a horizontal line in row i which connectsthe dot in column j to the one to its right in column j + 1orV i j indicates a vertical line in column i which connectsthe dot in row j to the one below in row j + 1Information for each line begins in column 1. The end of input is indicated by end-of-file. The firstrecord of the sample input below represents the board of the square above.OutputFor 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. Separateoutput for successive input records by a line of asterisks between two blank lines, like in the samplebelow.
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.
因为“**********************************”打印错了地方,导致wa了好几次。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define Max 1010
int main()
{
int count=0,n,m,x,y;
int v[11][11],h[11][11];
while(~scanf("%d",&n))
{
count++;
memset(v,0,sizeof v);
memset(h,0,sizeof h);
char c;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
cin>>c>>x>>y;
if(c=='H')
h[x][y]++;
else
v[y][x]++;
}
bool flagg=1;
if(count!=1)
printf("\n**********************************\n\n");
printf("Problem #%d\n\n",count);
for(int k=1;k<n;k++ )
{
int num=0;
for(int i=1;i<=n-k;i++ )
for(int j=1;j<=n-k;j++ )
{
bool flag=1;
for(int b=0;flag&&b<k;b++ )
if(!v[i+b][j+k]||!v[i+b][j])
flag=0;
for(int b=0;flag&&b<k;b++ )
if(!h[i+k][j+b]||!h[i][j+b])
flag=0;
if(flag)
num++;
}
if(num)
{
cout<<num<<" square (s) of size "<<k<<endl;
flagg=0;
}
}
if(flagg)
printf("No completed squares can be found.\n");
}
return 0;
}