poj 1020(回溯+dfs)
作者:互联网
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; int d[11],n,s,visit[50]; bool dfs(int num){ int i,j,wide; if(num==n)return true; int pos; int minx = 100; for(j=1;j<=s;j++){ if(minx>visit[j]){ pos = j; minx = visit[j]; } } for(i=10;i>0;i--){ if(d[i]==0)continue; if(s-visit[pos]>=i&&pos+i-1<=s){ wide = 0; for(j=pos;j<pos+i;j++){ // if(visit[j]<=visit[pos]){ // wide++; // continue; // } // break; if(visit[j]>visit[pos])break; wide++; } if(wide==i){ d[i]--; for(j=pos;j<pos+i;j++) visit[j] += i; // for(j=1;j<=s;j++) // cout<<visit[j]<<" "; // cout<<endl<<num<<endl; if(dfs(num+1))return true; d[i]++; for(j=pos;j<pos+i;j++) visit[j] -= i; } } } return false; } int main(){ int t,i,tmp,count,sum; scanf("%d",&t); while(t--){ scanf("%d%d",&s,&n); memset(d,0,sizeof(d)); memset(visit,0,sizeof(visit)); count = 0; sum = 0; for(i=0;i<n;i++){ scanf("%d",&tmp); if(tmp>s/2)count++; d[tmp]++; sum += tmp*tmp; } if(count>1||s*s!=sum){ printf("HUTUTU!\n"); } // for(i=0;i<=10;i++) // cout<<d[i]<<endl; else if(dfs(0))printf("KHOOOOB!\n"); else printf("HUTUTU!\n"); } return 0; }
标签:tmp,wide,1020,int,visit,pos,dfs,poj,include 来源: https://www.cnblogs.com/stevenzrx/p/15092510.html