Flexible Structure of poj 1020 Anniversary Cake dfs

Source: Internet
Author: User

Flexible Structure of poj 1020 Anniversary Cake dfs

Question:

For the length of a target Square and the side length of n small squares, ask whether the n small squares can be used to fill the target square.

Analysis:

Dfs does not necessarily set the order of placement at the beginning, but uses the number of entries already placed to represent the deep search dimension.

Code:

 

#include 
 
  using namespace std;int box_size;int n;int size_num[16];int col[64];bool dfs(int cnt){if(cnt==n)return true;int minx=INT_MAX,col_index;for(int i=1;i<=box_size;++i)if(minx>col[i]){minx=col[i];col_index=i;}for(int size=10;size>=1;--size){if(!size_num[size]) continue;if(box_size-col[col_index]>=size&&box_size-col_index+1>=size){int t=0;for(int c=col_index;c<=col_index+size-1;++c){if(col[c]==col[col_index]){++t;continue;}break;}if(t==size){size_num[size]--;for(int c=col_index;c<=col_index+size-1;++c)col[c]+=size;if(dfs(cnt+1)) return true;size_num[size]++;for(int c=col_index;c<=col_index+size-1;++c)col[c]-=size;}}}return false;   }int main(){int cases;scanf("%d",&cases);while(cases--){memset(size_num,0,sizeof(size_num));memset(col,0,sizeof(col));scanf("%d%d",&box_size,&n);int cnt=0,area=0;for(int i=1;i<=n;++i){int size;scanf("%d",&size);++size_num[size];area+=size*size;if(size>box_size/2) ++cnt;}if(cnt>1||area!=box_size*box_size){puts("HUTUTU!");continue;}if(dfs(0)) puts("KHOOOOB!");else puts("HUTUTU!");}return 0;} 
 


 

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.