poj 1020 Anniversary Cake (搜索)

15 篇文章 0 订阅
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<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值