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 1Sample Output
KHOOOOB!
HUTUTU!
看到這種有關(guān)圖的題目就有點(diǎn)頭疼,仔細(xì)研究了一下,最終的方案是優(yōu)先填滿每一行攘须,填滿了之后在去下一行搜索呢蔫。
剪枝策略還是老生常談的幾個(gè):
1.如果總面積不同直接錯(cuò)誤,
2.相同長(zhǎng)度的不要重復(fù)搜索堪滨,
3.每次搜索沒(méi)填滿的行從當(dāng)前y軸高度開(kāi)始搜索懂盐。(讓我的時(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;;
}
//從上面開(kāi)始填格子
if (dfs(maxSize, 1, 1)) {
cout << "KHOOOOB!" << endl;
}
else {
cout << "HUTUTU!" << endl;
}
}
return 0;
}