Anniversary (DFS)

本文介绍了一种解决正方形蛋糕是否能被切成特定大小小块的算法,通过深度优先搜索(DFS)策略,确保每个小块都是正方形且没有浪费。文章详细解释了算法思路,包括如何选择填充顺序和条件,以及如何判断解决方案的存在性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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!

题意:问n个小正方型能否组成一个面积为side*side的大正方

思路:见代码

/*思路: DFS
1.我选择按列来放,保证每次填充时选则填充的列剩余未填充的行数>=指定蛋糕的边长
2.假设我们的列的编号是从1开始的,那么我们就要保证当前选定列+选择填充的蛋糕边长-1<=蛋糕的边长
3.有两种情况是直接可以判断结果为"HUTUTU!"的
  1)所有小蛋糕面积的总和!=大蛋糕的面积
  2)存在一个以上小蛋糕的边长>大蛋糕边长/2
*/
#include<bits/stdc++.h>
using namespace std;
const int Max=1e3+5;
int pos[100],cake[20],side,n; //pos记录第i列的开销,cake记录各种边长蛋糕的个数
/*首先明确填充的顺序:
  1.从左往右填充
  2.从下到上填充
  3.选择蛋糕先大后小
*/
bool DFS(int number)
{
    if(number==n)
        return true;
    int Min=Max;
    int index;
    for(int i=1;i<=side;i+=1)
    {
        if(Min>pos[i])
        {
            Min=pos[i];
            index=i;
        }
    }
    for(int i=10;i>=1;i--)
    {
        /* if((如果不存在边长为i的蛋糕)||(选定列已填充的宽度+将要填充的宽度>给定蛋糕的尺寸)||
           (从当前列开始向右填充的长度>给定蛋糕的尺寸)) 跳过;
        */
        if(!cake[i]||Min+i>side||index+i-1>side)
            continue;
        bool sign=true;
        for(int x=index;x<=index+i-1;x+=1)
        {
            /*从选定列遍历到终止的列,若其中发现某列的开销大于选定列的开销则放弃此次填充
              (原因:因为实际上填充的是一个完整正方形)
            */
            if(pos[x]>Min)
            {
                sign=false;
                break;
            }
        }
        if(!sign)
            continue;
        cake[i]--;
        for(int x=index;x<=index+i-1;x+=1)
            pos[x]+=i;
        if(DFS(number+1))
            return true;
        cake[i]+=1;
        for(int x=index;x<=index+i-1;x+=1)
            pos[x]-=i;
    }
    return false;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        int pie[20];
        cin>>side>>n;
        int total=0;
        int Count=0;
        memset(cake,0,sizeof(cake));
        memset(pos,0,sizeof(pos));
        for(int i=0;i<n;i+=1)
        {
            cin>>pie[i];
            cake[pie[i]]+=1;
            total+=pie[i]*pie[i];
            if(pie[i]>side/2)
                Count+=1;
        }
        if(total!=side*side||Count>1)
            cout<<"HUTUTU!"<<endl;
        else
        {
            if(DFS(0))
                cout<<"KHOOOOB!"<<endl;
            else
                cout<<"HUTUTU!"<<endl;
        }
    }
    return 0;
}

 

资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 在苹果的生态系统中,IAP(应用内购买)是苹果应用商店(App Store)中应用开发者常采用的一种盈利模式,允许用户在应用内直接购买虚拟商品或服务。苹果为开发者提供了一份详细的人民币(CNY)IAP定价表,这份定价表具有以下特点: 价格分级:定价表由多个价格等级组成,开发者可根据虚拟商品的价值选择相应等级,等级越高,价格越高。例如,低等级可能对应基础功能解锁,高等级则对应高级服务或大量虚拟道具。 税收与分成:苹果会从应用内购买金额中抽取30%作为服务费或佣金,这是苹果生态的固定规则。不过,开发者实际到手的收入会因不同国家和地区的税收政策而有所变化,但定价表中的价格等级本身是固定的,便于开发者统一管理。 多级定价策略:通过设置不同价格等级,开发者可以根据商品或服务的类型与价值进行合理定价,以满足不同消费能力的用户需求,从而最大化应用的总收入。例如,一款游戏可以通过设置不同等级的虚拟货币包,吸引不同付费意愿的玩家。 特殊等级:除了标准等级外,定价表还包含备用等级和特殊等级(如备用等级A、备用等级B等),这些等级可能是为应对特殊情况或促销活动而设置的额外价格点,为开发者提供了更灵活的定价选择。 苹果IAP定价表是开发者设计应用内购机制的重要参考。它不仅为开发者提供了标准的收入分成模型,还允许开发者根据产品特性设定价格等级,以适应市场和满足不同用户需求。同时,开发者在使用定价表时,还需严格遵守苹果的《App Store审查指南》,包括30%的分成政策、使用苹果支付接口、提供清晰的产品描述和定价信息等。苹果对应用内交易有严格规定,以确保交易的透明性和安全性。总之,苹果IAP定价表是开发者在应用内购设计中不可或缺的工具,但开发者也需密切关注苹果政策变化,以确保应用的合规运营和收益最大化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值