UVA 201 Squares(暴力)

A children’sboard game consists of a square array of dots that contains lines connectingsome of the pairs of adjacent dots. One part of the game requires that theplayers count the number of squares of certain sizes that are formed by theselines. For example, in the figure shown below, there are 3 squares — 2 of size1 and 1 of size 2. (The “size” of a square is the number of lines segmentsrequired to form a side.)

Your problem is to write a program thatautomates the process of counting all the possible squares.

Input

The input file representsa series of game boards. Each board consists of a description of a square arrayof n2 dots (where 2 ≤ n ≤ 9) and some interconnectinghorizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted asfollows:

Line 1:              n                 thenumber of dots in a single row or column of the array

Line 2:      m     thenumber 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

Vi j          indicatesa vertical line in column i whichconnects the dot in row j to the onebelow in row j + 1

Information for eachline begins in column 1. The end of input is indicated by end-of-file. Thefirst record of the sample input below represents the board of the squareabove.

Output

For each record, label the correspondingoutput with ‘Problem #1’,‘Problem #2’, and so forth. Outputfor a record consists of the number of squares of each size on the board, fromthe smallest to the largest. lf no squares of any size exist, your programshould print an appropriate message indicating so. Separate output for successiveinput records by a line of asterisks between two blank lines, like in thesample 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.



【思路】

用两个数组记录横向、纵向边,以正方形左上顶点为正方形位置,分别枚举不同size的正方形查看四周是否完整,计数即可。读入的纵向边很迷,须和图对应好。


【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 10;

int n, m;
bool hor[MAXN][MAXN], ver[MAXN][MAXN];
int ans[MAXN];

int main()
{
    int kase = 0;
    while (scanf("%d %d", &n, &m) == 2) {
        memset(hor, false, sizeof(hor));
        memset(ver, false, sizeof(ver));
        memset(ans, 0, sizeof(ans));
        char mes[2];
        int a, b;
        for (int i = 1; i <= m; i++) {
            scanf("%s %d %d", mes, &a, &b);
            if (mes[0] == 'H') hor[a][b] = true;
            if (mes[0] == 'V') ver[b][a] = true;
        }
        if (++kase != 1) printf("\n**********************************\n\n");
        printf("Problem #%d\n\n", kase);
        bool flag = false;
        for (int i = 1; i <= n - 1; i++) {
            int cnt = 0;
            for (int j = 1; j <= n - i; j++)
                for (int k = 1; k <= n - i; k++) {
                    bool ok = true;
                    for (int h = j; h <= j + i - 1; h++)
                        if (!ver[h][k] || !ver[h][k + i]) ok = false;
                    for (int v = k; v <= k + i - 1; v++)
                        if (!hor[j][v] || !hor[j + i][v]) ok = false;
                    if (ok) cnt++;
                }
            if (cnt != 0) {
                flag = true;
                ans[i] = cnt;
            }
        }
        if (!flag)
            printf("No completed squares can be found.\n");
        else {
            for (int i = 1; i <= n - 1; i++)
                if (ans[i] != 0) printf("%d square (s) of size %d\n", ans[i], i);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值