题目地址:http://jobdu.sinaapp.com/problem.php?cid=1040&pid=86
C语言源码:
#include<stdio.h>#define maxsize 10000000int queue[10000000][3];int mark[110][110][110];int main(){ int s,n,m,i,j,k,front,rear,flag,num,f; scanf("%d %d %d",&s,&n,&m); while(s||n||m) { for(i=0;i<110;i++) for(j=0;j<110;j++) for(k=0;k<110;k++) mark[i][j][k]=0; front=0; rear=1; flag=1; num=0; f=-1; queue[0][0]=s; queue[0][1]=0; queue[0][2]=0; if(s%2==1) printf("NO\n"); else { while(front!=rear) { i=queue[front][0]; j=queue[front][1]; k=queue[front][2]; front=(front+1)%maxsize; if(mark[i][j][k]!=1) { mark[i][j][k]=1; if((i==s/2&&j==s/2)||(i==s/2&&k==s/2)||(j==s/2&&k==s/2)) { f=1; break; } if((i<=n-j)&&mark[0][j+i][k]==0) { queue[rear][0]=0; queue[rear][1]=j+i; queue[rear][2]=k; rear=(rear+1)%maxsize; } else if((i>n-j)&&(mark[i-(n-j)][n][k]==0)) { queue[rear][0]=i-(n-j); queue[rear][1]=n; queue[rear][2]=k; rear=(rear+1)%maxsize; } if((i<=m-k)&&mark[0][j][k+i]==0) { queue[rear][0]=0; queue[rear][1]=j; queue[rear][2]=k+i; rear=(rear+1)%maxsize; } else if((i>m-k)&&(mark[i-(m-k)][j][m]==0)) { queue[rear][0]=i-(m-k); queue[rear][1]=j; queue[rear][2]=m; rear=(rear+1)%maxsize; } if((j<=s-i)&&mark[i+j][0][k]==0) { queue[rear][0]=i+j; queue[rear][1]=0; queue[rear][2]=k; rear=(rear+1)%maxsize; } else if((j>s-i)&&(mark[s][j-(s-i)][k]==0)) { queue[rear][0]=s; queue[rear][1]=j-(s-i); queue[rear][2]=k; rear=(rear+1)%maxsize; } if((j<=m-k)&&mark[i][0][k+j]==0) { queue[rear][0]=i; queue[rear][1]=0; queue[rear][2]=k+j; rear=(rear+1)%maxsize; } else if((j>m-k)&&(mark[s][j-(m-k)][m]==0)) { queue[rear][0]=i; queue[rear][1]=j-(m-k); queue[rear][2]=m; rear=(rear+1)%maxsize; } if((k<=s-i)&&mark[i+k][j][0]==0) { queue[rear][0]=i+k; queue[rear][1]=j; queue[rear][2]=0; rear=(rear+1)%maxsize; } else if((k>s-i)&&(mark[s][j][k-(s-i)]==0)) { queue[rear][0]=s; queue[rear][1]=j; queue[rear][2]=k-(s-i); rear=(rear+1)%maxsize; } if((k<=n-j)&&mark[i][j+k][0]==0) { queue[rear][0]=i; queue[rear][1]=j+k; queue[rear][2]=0; rear=(rear+1)%maxsize; } else if((k>n-j)&&(mark[i][n][k-(n-j)]==0)) { queue[rear][0]=i; queue[rear][1]=n; queue[rear][2]=k-(n-j); rear=(rear+1)%maxsize; } } if(front==flag) { flag=rear; num++; } } if(f==-1) printf("NO\n"); else printf("%d\n",num); } scanf("%d %d %d",&s,&n,&m);; }}