Squares UVa201

Time: 45mins.

#include <iostream>
#include <cstring>
using namespace std;
int h[20][20] = { 0 }, v[20][20] = { 0 }, res[20] = { 0 };
int main()
{
    int kase = 0, size, T;
    while (cin >> size >> T)
    {
        memset(h, 0, sizeof h);
        memset(v, 0, sizeof v);
        memset(res, 0, sizeof res);
        while (T--)
        {
            char s;
            int x, y;
            cin >> s >> x >> y;
            if (s == 'H')
                h[x][y] = 1;
            else
                v[y][x] = 1;
        }
        int flag = 1;//标记是否完全无解
        for (int s = 1; s <= size; s++)
        {
            int cnt = 0;
            for (int i = 1; i <= size - s; i++)
                for (int j = 1; j <= size - s; j++)
                {
                    int mark = 1;//标记是否缺少边
                    for (int x = j; x <= j + s - 1; x++)
                        if (!h[i][x] || !h[i + s][x])
                            mark = 0;

                    for (int x = i; x <= i + s - 1; x++)
                        if (!v[x][j] || !v[x][j + s])
                            mark = 0;
                    if (mark)
                        cnt++;
                }
            res[s] = cnt;
            if (cnt)
                flag = 0;
        }
        if(kase)
            cout << endl << "**********************************" << endl << endl;
        cout << "Problem #" << ++kase << endl << endl;
        if (flag)
            cout << "No completed squares can be found." << endl;
        for (int i = 1; i <= size; i++)
            if (res[i])
                cout << res[i] << " square (s) of size " << i << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值