poj1020 搜索

23 篇文章 0 订阅

 

如题:http://poj.org/problem?id=1020

Anniversary Cake
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15696 Accepted: 5120

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!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

 

 

一开始的思路是毫无剪枝的强行深搜,虽然知道应该会超,但还是试了试。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n,m;
int need[20];
int map[200][200];

int cmp(int &a,int &b)
{
 return a>b;
}

int judge(int x,int y,int pos)
{
 int i,j;
 if(x+need[pos]-1>n||y+need[pos]-1>n)
  return 0;
 for(i=x;i<x+need[pos];i++)
  for(j=y;j<y+need[pos];j++)
   if(map[i][j])
    return 0;
 return 1;
}

int dfs(int pos) //将第pos个小正方形放进
{
 if(pos==m)
 {
  return 1;
 }
 int i,j,k,l;
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
  {
   if(judge(i,j,pos))
   {
    for(k=i;k<i+need[pos];k++)
     for(l=j;l<j+need[pos];l++)
      map[k][l]=1;
    if(dfs(pos+1))
     return 1;
    for(k=i;k<i+need[pos];k++)
     for(l=j;l<j+need[pos];l++)
      map[k][l]=0;
   }
  }
 return 0;
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
 int t;
 cin>>t;
 int i;
 while(t--)
 {
  scanf("%d",&n);
  scanf("%d",&m);
  int s=0;
  for(i=0;i<m;i++)
  {
   scanf("%d",&need[i]);
   s+=need[i]*need[i];
  }
  if(s!=n*n)
  {
   printf("HUTUTU!\n");
   continue;
  }
  sort(need,need+m,cmp);
  memset(map,0,sizeof(map));
  if(dfs(0))
   printf("KHOOOOB!\n");
  else
   printf("HUTUTU!\n");
 }
 return 0;
}

 

 

之后看了别人的报告,受益匪浅。

每一次找到一个合适的位置,放进这个位置能放的最大一个方块。

col[i]数组代表第i列里有单位为1的蛋糕块的数量。

放蛋糕的方法:从左到右扫描出列,单位蛋糕最少的那一列,然后选择一个能放入的尽量大的放块放入。小方块最后放。

通过col数组和一种放方块的正确方法大大减少了搜索的复杂度。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int n,m;
int sizenum[11];
int col[200];

int dfs(int num) //已经装入的蛋糕数量。满足每次选一个最大的装入。从
{
 if(num==m)
  return 1;
 int mincol=0;
 int min=200;
 int i,j;
 for(i=1;i<=n;i++)
 {
  if(col[i]<min)
  {
   min=col[i];
   mincol=i;
  }
 }
 for(i=10;i>=1;i--)
 {
  if(sizenum[i]&&n-min>=i&&n-mincol+1>=i)
  {
   int w=1;
   for(j=mincol+1;j<mincol+i;j++)
   {
    if(col[j]<=min)
    {
     w++;
     continue;
    }
    break; //不连续.
   }
   if(w>=i)
   {
    sizenum[i]--;
    for(j=mincol;j<mincol+i;j++)
     col[j]+=i;
    if(dfs(num+1))
     return 1;
    sizenum[i]++;
    for(j=mincol;j<mincol+i;j++)
     col[j]-=i;
   }
  }
 }
 return 0;
}

int main()
{
// freopen("C:\\1.txt","r",stdin);
 int t;
 cin>>t;
 int i;
 while(t--)
 {
  memset(sizenum,0,sizeof(sizenum));
  memset(col,0,sizeof(col));
  cin>>n;
  cin>>m;
  int s=0;
  for(i=0;i<m;i++)
  {
   int size;
   scanf("%d",&size);
   sizenum[size]++;
   s+=size*size;
  }
  if(s!=n*n)
  {
   printf("HUTUTU!\n");
   continue;
  }
  if(dfs(0))
   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、付费专栏及课程。

余额充值