1990 ICPC WF B Squares【暴力】

题目链接:UVa 201 Squares

Description:
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.

题目大意:
有n行n列的小黑点,还有m条线段链接其中的一些黑点。统计这些线段连成了多少正方形(各种边长分别统计)。
行从上到下编号为1~n,列从左到右编号为1~n。边用H i j和V i j表示,分别代表边(i,j)-(i,j+1)和边(j,i)-(j+1,i)。
(题目中给出的图最左边的线段是V 1 1,图中包含两个边长为1的正方形和一个边长为2的正方形。
解题思路:
因为数据范围不是很大,所以我直接标记出横着的每条边覆盖到的点以及竖着的每条边能覆盖到的点,然后枚举以每个点为左上角顶点时所能组成的正方形的情况。
其中记录边覆盖的点时用了四维数组,前两维表示起点坐标,后两维表示终点坐标。(其实可以用二维的代表每个边,感觉我这样显得有些笨拙。

Mycode:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 1005;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;

map<int,int> a;
bool xx[10][10][10][10];
bool yy[10][10][10][10];

int main()
{
    int n, _;
    char op;
    int x, y, cas = 0;
    while(cin >> n >> _)
    {
        if(cas)
            puts("\n**********************************\n");
        a.clear();
        memset(xx, false, sizeof(xx));
        memset(yy, false, sizeof(yy));
        while(_--)
        {
            cin >> op >> x >> y;
            if(op == 'H')
                xx[x][y][x][y+1] = true;
            else
                yy[y][x][y+1][x] = true;
        }

        for(int i = 1; i < n; ++i)
        {
            for(int j = 1; j < n; ++j)
            {
                for(int k = 1; k <= n; ++k)
                {
                    if(k + j > n) break;
                    if(k + i > n) break;
                    int q;
                    for(q = i; q < i+k; ++q)
                    {
                        if(yy[q][j][q+1][j] == 0 || yy[q][j+k][q+1][j+k] == 0) break;
                    }
                    if(q < i+k) continue;;

                    for(q = j; q < j+k; ++q)
                    {
                        if(xx[i][q][i][q+1] == 0 || xx[i+k][q][i+k][q+1] == 0) break;
                    }
                    if(q < j+k) continue;;

                    //printf("i=%d j=%d k=%d\n",i,j,k);
                    //cout << k << endl;
                    if(!a.count(k)) a[k] = 0;
                    ++a[k];
                }
            }
        }

        //cout << "a.size=" << a.size() << endl;
        printf("Problem #%d\n\n",++cas);
        if(a.size())
        {
            for(auto it = a.begin(); it != a.end(); ++it)
                printf("%d square (s) of size %d\n", it->second,it->first);
        }
        else
            puts("No completed squares can be found.");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值