北大oj1020——Anniversary Cake

Anniversary Cake

Time Limit: 1000MS Memory Limit: 10000K

Description

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

Sample Input

2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1

Sample Output

KHOOOOB!
HUTUTU!

看到这种有关图的题目就有点头疼,仔细研究了一下,最终的方案是优先填满每一行,填满了之后在去下一行搜索。
剪枝策略还是老生常谈的几个:
1.如果总面积不同直接错误,
2.相同长度的不要重复搜索,
3.每次搜索没填满的行从当前y轴高度开始搜索。(让我的时间从172ms->47ms)
4.不要每次都去eachCake中判断是否都为0。(让我的时间从47ms->16ms)

#include<iostream>
#include<algorithm>
using namespace std;

int cakeArray[41][41];
int eachCake[11];
int m, n;
int maxSize;
int totalCakeCnt;

void addCake(int x, int y, int cakeSize) {
    for (int _y = y; _y < y + cakeSize; _y++) {
        for (int _x = x; _x < x + cakeSize; _x++) {
            cakeArray[_y][_x] = 1;
        }
    }
}

void removeCake(int x, int y, int cakeSize) {
    for (int _y = y; _y < y + cakeSize; _y++) {
        for (int _x = x; _x < x + cakeSize; _x++) {
            cakeArray[_y][_x] = 0;
        }
    }
}

int dfs(int index, int x, int y) {
    eachCake[index] --;
    totalCakeCnt--;
    if (totalCakeCnt==0) {
        return 1;
    }

    addCake(x, y, index);


    int newX, newY, maxLast;
    for (int _y = y; _y <= m; _y++) {
        //寻找当前行剩余最大长度
        newY = _y;
        maxLast = 0;
        bool start = false;
        for (int _x = 1; _x <= m; _x++) {
            if (cakeArray[_y][_x] == 0) {
                if (!start) {
                    start = true;
                    newX = _x;
                }
                maxLast++;
            }
            else {
                if (start) {
                    break;
                }
            }
        }
        if (maxLast > 0) {
            break;
        }
    }
    for (int i = min(maxLast, m - newY + 1); i > 0; i--) {
        if (eachCake[i] > 0 && dfs(i, newX, newY)) {
            return 1;
        }
    }
    
    eachCake[index] ++;
    totalCakeCnt++;
    removeCake(x, y, index);
    return 0;
}

int main() {
    int t;
    cin >> t;
    for (int _t = 0; _t < t; _t++) {
        cin >> m >> n;
        int totalSize = 0;
        maxSize = 0;
        totalCakeCnt = n;
        memset(eachCake, 0, sizeof(eachCake));
        memset(cakeArray, 0, sizeof(cakeArray));
        for (int _n = 0; _n < n; _n++) {
            int each;
            cin >> each;
            eachCake[each] ++;
            totalSize += each * each;
            if (each > maxSize) {
                maxSize = each;
            }
        }

        if (totalSize != m*m) {
            cout << "HUTUTU!" << endl;
            continue;;
        }

        //从上面开始填格子
        if (dfs(maxSize, 1, 1)) {
            cout << "KHOOOOB!" << endl;
        }
        else {
            cout << "HUTUTU!" << endl;
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子糖莫莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值