北大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!

看到這種有關(guān)圖的題目就有點(diǎn)頭疼,仔細(xì)研究了一下,最終的方案是優(yōu)先填滿每一行,填滿了之后在去下一行搜索。
剪枝策略還是老生常談的幾個(gè):
1.如果總面積不同直接錯(cuò)誤,
2.相同長(zhǎng)度的不要重復(fù)搜索,
3.每次搜索沒填滿的行從當(dāng)前y軸高度開始搜索。(讓我的時(shí)間從172ms->47ms)
4.不要每次都去eachCake中判斷是否都為0。(讓我的時(shí)間從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++) {
        //尋找當(dāng)前行剩余最大長(zhǎng)度
        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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,835評(píng)論 0 10
  • 四六級(jí)剛結(jié)束就急著對(duì)答案??? 話說還記得自己在考場(chǎng)上寫了啥嗎??? 其實(shí)你只要只知道這3個(gè)答案就行:黃山=Hua...
    知米閱讀的知米妞閱讀 3,044評(píng)論 0 0
  • 彩排完,天已黑
    劉凱書法閱讀 4,465評(píng)論 1 3
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,489評(píng)論 2 7
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,633評(píng)論 0 4

友情鏈接更多精彩內(nèi)容