题目地址:http://www.tsinsen.com/ViewGProblem.page?gpid=A1095
//清橙OJ A1095 回溯之教室排课
//http://www.tsinsen.com/ViewGProblem.page?gpid=A1095
#include<stdio.h>
#include<string.h>
int room[9]={0,120,40,85,50,100,140,70,100};
int mark[9],ans[5],demand[5],flag;
void RE(int k)
{
int i;
if(k==5)
{
flag=0;
for(i=1;i<4;i++)printf("%d ",ans[i]);
printf("%d\n",ans[4]);
return;
}
for(i=1;i<=8;i++)
{
if(mark[i]&&demand[k]<=room[i])
{
mark[i]=0;
ans[k]=i;
RE(k+1);
mark[i]=1;
}
}
}
int main()
{
while(~scanf("%d %d %d %d",&demand[1],&demand[2],&demand[3],&demand[4]))
{
flag=1;
memset(mark,1,9*sizeof(int));
RE(1);
if(flag)printf("-1\n");
}
return 0;
}