ACM/ICPC World Finals 1990 && UVA 201 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 gure 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 le represents a series of game boards. Each board consists of a description of a square array
of
n
2
dots (where 2

n

9) and some interconnecting horizontal and vertical lines. A record for a
single board with
n
2
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- le. The rst
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.

题目大意:给出一个n*n的点阵,并将点与点之间连线,数出不同单位长度的正方形个数。此处遍历所有可能的正方形左上角顶点,将j(x坐标),k(y坐标),i(边长)传入done函数,判断是否能构成正方形。
AC代码如下,输入方式,输出格式是个坑,注意读题!!!

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct dot{
    int h,v;
};
int n;
dot adot[600];
int done(int x,int y,int len){
    int flag=1;
    for(int i=y;i<y+len;i++){
        if(adot[(x-1)*n+i].h==0 || adot[(x+len-1)*n+i].h==0 ) flag=0;
    }
    for(int j=x;j<x+len;j++){
        if(adot[(j-1)*n+y].v==0 || adot[(j-1)*n+(y+len)].v==0) flag=0;
    }
    return flag;
}
int main(){
    int sum,x,y,ans,pro;
    char s;
    pro=0;
    while(~scanf("%d",&n)){
        pro++;
        if(pro!=1) printf("\n**********************************\n\n");
        for(int i=1;i<=n*n;i++){
            adot[i].v=0;
            adot[i].h=0;
        }
        cin>>sum;
        for(int i=0;i<sum;i++){
            cin>>s>>x>>y;
            if(s=='V')
                adot[n*(y-1)+x].v=1;
            else if(s=='H')
                adot[n*(x-1)+y].h=1;
        }
        /*
         for(int i=1;i<=n*n;i++){
         printf("adot[%d] h=%d v=%d\n",i,adot[i].h,adot[i].v);
         }*/
        printf("Problem #%d\n\n",pro);
        int find=0;
        for(int i=1;i<=n-1;i++){
            ans=0;
            for(int j=1;j<=n-i;j++){
                for(int k=1;k<=n-i;k++){
                    ans+=done(j,k,i);
                    //if(done(j,k,i)) printf("x=%d y=%d len=%d\n",j,k,i);
                }
            }
            if(ans>0){
                find=1;
                printf("%d square (s) of size %d\n",ans,i);
            }
        }
        if(find ==0) printf("No completed squares can be found.\n");

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值