UVA10502 Counting Rectangles水题

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10502 Counting Rectangles水题相关的知识,希望对你有一定的参考价值。

In some tests there appears the problem of finding the number of rectangles (or circles, or triangles, …) of different sizes in a figure. We consider the problem of counting rectangles (including squares) in a rectangular board.
    Given a rectangular board with n rows and m columns, with valid possitions marked with ‘1’ and non valid possitions marked with ‘0’, we want to count the number of rectangles (including squares) in the board formed by squares marked with ‘1’.
Input
The input will consist of a series of problems, with each problem in a serie of lines. In the first and second lines the rows (n) and columns (m) of the board are indicated, in the next n lines the board in represented, with a row of the board in each line, and m ‘0’ or ‘1’ (without spaces) in each line. When the input of a problem finishes the next problem begins in the next line. The input finishes when 0 appears as the dimension of the board. Both dimensions of the board are less than or equal to 100.
Output
The solution of each problem appears in a line, without separation between lines. For example, in the board
11
01
the rectangles are:

1-    -1    --    11    -1
--    --    -1    --    -1

Sample Input
2
2
11
01
4
3
110
101
111
011
0
Sample Output
5
22

问题链接UVA10502 Counting Rectangles
问题简述:计算一个01构成的矩阵中,有多少个不同的矩形?
问题分析:简单问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10502 Counting Rectangles */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
char g[N][N + 1];
int sum[N];

int main()
{
    int n, m;
    while (~scanf("%d", &n) && n) {
        scanf("%d", &m);
        for (int i = 0; i < n; i++)
            scanf("%s", g[i]);

        int ans = 0, length = 0, width = 0, t = 0;
        for (int i = 0; i < n; i++) {
            memset(sum, 0, sizeof sum);
            for (int j = i; j < n; j++)
                for (int k = 0; k < m; k++) {
                    sum[k] += g[j][k] - '0';
                    if (k == 0 || t != length * width)
                        t = 0, length = 0;
                    t += sum[k];
                    length++, width = j - i + 1;
                    if (t == length * width)
                        ans += length;
                }
        }

        printf("%d\\n", ans);
    }

    return 0;
}

以上是关于UVA10502 Counting Rectangles水题的主要内容,如果未能解决你的问题,请参考以下文章

Digit Counting UVA - 1225

UVA 1225 Digit Counting(统计数位出现的次数)

UVA10198 Counting大数+递推

UVA11401-Triangle Counting-递推

UVA10516 Another Counting Problem大数+递推

UVA10516 Another Counting Problem大数+递推