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!
看到这种有关图的题目就有点头疼,仔细研究了一下,最终的方案是优先填满每一行,填满了之后在去下一行搜索。
剪枝策略还是老生常谈的几个:
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;
}