POJ 1020 Anniversary Cake (cut square cake + search)

Source: Internet
Author: User

Anniversary CakeNahid 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 is Square-sha PED). 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 dat A For each test case. Each test case consist of a containing a integer s, the side of the cake, and followed by an integer n (1≤n≤1 6), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should is one output line per test case containing one of the words khoooob! or hututu! Depending on whether the cake can is cut into pieces of specified size without any waste or not.

Sample Input

24 8 1 1 1 1 1 3 1 15 6 3 3 2 1 1 1

Sample Output

khoooob! hututu!

Test instructions: Cut a big square cake into small squares and ask if you can do it.
Analysis: First judge all the small square area of the sum is equal to the large square, not sure. And if there are two largest Bengchang and larger squares in a small square, surely not. Search, because the side length of the small square is 10, and the maximum is only 16, so the length of the large square is 40
Then I start from the bottom layer to the top, with a col[] array to save this column corresponds to the height, to be paved is always looking for col[] the smallest leftmost (the lowest place) also to determine whether you can lay a square, until all square paved.

The code is as follows:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<set>#include<map>#include<queue>#include<vector>#include<iterator>#include<utility>#include<sstream>#include<iostream>#include<cmath>#include<stack>UsingNamespace Std;Constint INF=1000000007;ConstDouble EPS=0.00000001;int EdgeN;int H[11],col[41];boolDfs(int cur){If(cur==n)ReturnTrue; Finish the shop.int MinX=50Pos;For(int I=1; I<=edge; I++)If(col[I]<minx){MinX=col[I],pos=i;}//Find the lowest and most left positionFor(int I=10; I>=1; I--)//enumeration of Square heights{If(! H[I])Continue;If(MinX+i<=edge&&pos+i<=edge+1)//cannot cross the border{BOOL OK=True;For(Int St=pos; St<pos+i; St+ +)//To determine if it is possible to shop{If(col[St]>col[POS]){OK=False;Break;}}If(OK){For(Int St=pos; St<pos+i; St+ +) col[St]+=i; The position of the shop all plus this height H[I]--; Remove the square.If(Dfs(cur+1))ReturnTrue; H[I]++;For(Int St=pos; St<pos+i; St+ +) col[St]-=i;}}}ReturnFalse;}IntMain(){int T; Cin>>t;While(T--){cin>>edge>>n;Memset(H,0,sizeof(H));Memset(col,0,sizeof(col));int sum=0Cnt=0;For(int I=1; I<=n; I++){int size; Cin>>size; H[Size]++; Sum+=size*size;If(Size>edge/2) CNT++;}If(CNT>1|| Edge*edge!=sum)//Pre-judgment{cout<< "hututu!" <<endl; continue;  if (dfs< Span class= "Sh-symbol" > (0<<  "khoooob!" <<endl; else cout<< "HUTUTU!" <<endl;  return 0;< Span class= "Sh-cbracket" >             

POJ 1020 Anniversary Cake (cut square cake + search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.