poj 1020 Anniversary Cake (搜索)

Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16592   Accepted: 5409

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!


// 总面积不能恰好和要求的恰好相等或者有边比要求的长 直接否
// 否则DFS 具体注释

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3f3f3f3f
#define zero 1e-8
using namespace std;

int fin, n;
int cake[100];
int flag;
int vis[100];//标记cake
bool map[100][100];//总蛋糕

void findnext(int &x, int &y)
{

    for (int i = 1; i <= fin; ++i)
        for (int j = 1; j <= fin; ++j) {
            if (!map[i][j]) {
                x = i;
                y = j;
                return ;
            }
        }
}

bool judge(int x, int y, int len)
{
    for(int i = 0; i < len; ++i)
        for (int j = 0; j < len; ++j)
            if (map[x + i][y + j]) return true ;

    return false;
}

void print(int ii, int jj, int x)
{
    for (int i = 0; i < x; ++i)
        for (int k = 0; k < x; ++k)
            map[ii + i][jj + k] = true;
}

void erease(int ii, int jj, int x)
{
    for (int i = 0; i < x; ++i)
        for (int k = 0; k < x; ++k)
            map[ii + i][jj + k] = false;
}

inline bool ifnext(int x, int y, int len)
{
    if (x + len - 1 > fin || y + len - 1 > fin || judge(x, y, len)) return false;

    return true;
}

void dfs(int num)
{


    if (flag) return ;

    if (!num) {
        flag = 1;
        return ;
    }
    int x, y;
    findnext(x, y);//找下一个的位置
    int last = -1;
    for (int i = 1; i <= n; ++i) {

        if (vis[i] || cake[i] == last) continue ;//已经选的或相同边长的过
        if (!ifnext(x, y, cake[i])) return ;//cake按小到大排序 这个不满足 直接返回
        vis[i] = true;
        print(x, y, cake[i]);
        dfs(num - 1);
        erease(x, y, cake[i]);
        vis[i] = false;
        last = cake[i];
    }

}
int main()
{
    int t;
    cin >> t;
    for (; t--;) {
        scanf("%d %d", &fin, &n);
 int tem = fin * fin;
        flag = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &cake[i]);
            tem -= cake[i] * cake[i];
            if (cake[i] > fin) flag = 1;
        }
        if (flag || tem) printf("HUTUTU!\n");
        else {
            memset(map, 0, sizeof(map));
            memset(vis, 0, sizeof(vis));
            sort(cake + 1, cake + n + 1);
            dfs(n);
            if (flag) printf("KHOOOOB!\n");
            else printf("HUTUTU!\n");
        }
    }
    return 0;
}


你可能感兴趣的:(C++,acm)