第四章 - uva201 - Squares

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.InputThe 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. Outputfor 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 Input416H 1 1H 1 3H 2 1H 2 2H 2 3H 3 2H 4 2H 4 3V 1 1V 2 1V 2 2V 2 3V 3 2V 4 1V 4 2V 4 323H 1 1H 2 1V 2 1Sample OutputProblem #12 square (s) of size 11 square (s) of size 2**********************************Problem #2No 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,k=0;
    char c;
    while(scanf("%d%d",&n,&m)!=EOF){
        getchar();
        memset(H,0,sizeof(H));
        memset(V,0,sizeof(V));

        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(k>0)printf("\n**********************************\n\n");
        printf("Problem #%d\n\n",++k);
        int sum=0;
        for(int s=1;s<=n;s++){
            c=0;
            for(int i=1;i+s<=n;i++){
                for(int j=1;j+s<=n;j++){
                    int flag=1;
                    for(int h=j;h<j+s;h++){
                        if(!H[i][h]||!H[i+s][h])flag=0;
                    }
                    for(int v=i;v<i+s;v++){
                        if(!V[v][j]||!V[v][j+s])flag=0;
                    }
                    if(flag)c++;
                }
            }
            if(c)printf("%d square (s) of size %d\n",c,s);
            sum+=c;
        }
        if(!sum)printf("No completed squares can be found.\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值