CLOCK - Chỉnh đồng hồ

Tác giả: hieult

Ngôn ngữ: C++

#include <stdio.h>
//#include <conio.h>
int d[81]={1,1,0,1,1,0,0,0,0,
1,1,1,0,0,0,0,0,0,
0,1,1,0,1,1,0,0,0,
1,0,0,1,0,0,1,0,0,
0,1,0,1,1,1,0,1,0,
0,0,1,0,0,1,0,0,1,
0,0,0,1,1,0,1,1,0,
0,0,0,0,0,0,1,1,1,
0,0,0,0,1,1,0,1,1};
long a[10],min,l,t;
void doi(int x,int y)
  {
  int i;
  for(i=1;i<=y;i++)
    a[x]=(a[x]+1)%4;
  }
void thunhan(long x)
  {
  int ok,i;
  if(x<min)
    {
    ok=1;
    for(i=1;i<=9;i++)
      if(a[i]!=0)
        {
        ok=0;
        break;
        }
    if(ok==1) min=x;
    }
  }      
void xuli(long i,long tong)
  {               
  int j,k;
  for(j=0;j<=3;j++)
    {
    tong=tong+j;
    for(k=1;k<=9;k++)
      if(d[9*(i-1)+k-1]==1)
        doi(k,j);
    if(i==9)
      thunhan(tong);
    else xuli(i+1,tong);
    tong=tong-j;
    for(k=1;k<=9;k++)
      if(d[9*(i-1)+k-1]==1)
        doi(k,4-j);              
    }
  }
main()
{
min=100000;
for(l=1;l<=3;l++)
  {
  scanf("%ld",&t);
  a[3*(l-1)+1]=t/100;
  a[3*(l-1)+3]=t%10;
  a[3*(l-1)+2]=(t/10)%10;
  }
xuli(1,0);
printf("%ld",min);
//getch();
}               

Download