您的位置:首页 > 其它

UVa 201 Squares(正方形)

2015-01-14 12:32 447 查看
Description







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 array  Line 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.



这个题,看完紫书的描述很快就有思路了,但是坑人的是,敲完代码怎么调试都不对,最后仔细阅读了一下原题,结果。。。紫书的描述又是错的!(给跪了啊,白白浪费时间啊)

思路很简单,用两个数组存水平线和竖直线,然后先枚举正方形的边长,然后对于每个边长枚举方格内的所有顶点,对于每个顶点,向右枚举处于同一水平边和对边的点,向下枚举处于同一竖直边和对边的点,如果枚举过程中任何一个顶点未在对应数组中有记录,说明该点不存在连线,自然不可组成正方形,否则遍历能正常结束,说明所有点都有连线,计数加1,输出当前边长的正方形数,代码如下:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
ios::sync_with_stdio(false);
int v[11][11],h[11][11];//分别存竖直线和水平线
int m,n,k=0;
while(k++,cin>>n>>m)
{
bool noans=true;//标记是否有正方形
memset(v,0,sizeof(v));
memset(h,0,sizeof(h));
char mode;
int a,b;
for(int i=0;i<m;i++)
{
cin>>mode>>a>>b;
if(mode=='H')
h[a][b]++;//将线段信息存入数组
else
v[b][a]++;//这里按照紫书的描述应该是v[a][b],但是原题不是这样。。。坑
}
if(k!=1)
cout<<endl<<"**********************************"<<endl<<endl;
cout<<"Problem #"<<k<<endl<<endl;
for(int s=1;s<n;s++)//枚举边长
{
int cont=0;
for(int i=1;i<=n-s;i++)//枚举左上顶点
for(int j=1;j<=n-s;j++)
{
bool mark=true;//标记当前顶点是否可能有当前边长的正方形
for(int x=i,y=j;mark&&x<i+s;x++)
if(!v[x][y]||!v[x][y+s])//任何一点无连线则不可能组成正方形
mark=false;
for(int x=i,y=j;mark&&y<j+s;y++)
if(!h[x][y]||!h[x+s][y])
mark=false;
if(mark)//可组成正方形则当前边长正方形数量累加
cont++;
}
if(cont)
{
cout<<cont<<" square (s) of size "<<s<<endl;
noans=false;
}
}
if(noans)//无结果
cout<<"No completed squares can be found."<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: