usaco1.42 The Clocks

做了几天,bfs一直超内存,dfs一直死循环,最后直接枚举过了,每个操作最多用3次,9重循环枚举每个操作的次数。

View Code
  1 /*

  2  ID: your_id_here

  3  PROG: clocks

  4  LANG: C++

  5  */

  6 #include <iostream>

  7 #include<cstdio>

  8 #include<cstring>

  9 using namespace std;

 10 int pjudge(int *x,int *a)

 11 {

 12     int i,j,k;

 13     int flag = 1;

 14     for(i = 1; i <= 9 ; i++)

 15     {

 16         switch(i)

 17         {

 18             case 1:a[1] = (a[1]+3*x[i])%12;

 19                    a[2] = (a[2]+3*x[i])%12;

 20                    a[4] = (a[4]+3*x[i])%12;

 21                    a[5] = (a[5]+3*x[i])%12;

 22                    break;

 23             case 2:a[1] = (a[1]+3*x[i])%12;

 24                    a[2] = (a[2]+3*x[i])%12;

 25                    a[3] = (a[3]+3*x[i])%12;

 26                    break;

 27             case 3:a[2] = (a[2]+3*x[i])%12;

 28                    a[3] = (a[3]+3*x[i])%12;

 29                    a[5] = (a[5]+3*x[i])%12;

 30                    a[6] = (a[6]+3*x[i])%12;

 31                    break;

 32             case 4:a[1] = (a[1]+3*x[i])%12;

 33                    a[4] = (a[4]+3*x[i])%12;

 34                    a[7] = (a[7]+3*x[i])%12;

 35                    break;

 36             case 5:a[2] = (a[2]+3*x[i])%12;

 37                    a[4] = (a[4]+3*x[i])%12;

 38                    a[5] = (a[5]+3*x[i])%12;

 39                    a[6] = (a[6]+3*x[i])%12;

 40                    a[8] = (a[8]+3*x[i])%12;

 41                    break;

 42             case 6:a[3] = (a[3]+3*x[i])%12;

 43                    a[6] = (a[6]+3*x[i])%12;

 44                    a[9] = (a[9]+3*x[i])%12;

 45                    break;

 46             case 7:a[4] = (a[4]+3*x[i])%12;

 47                    a[5] = (a[5]+3*x[i])%12;

 48                    a[7] = (a[7]+3*x[i])%12;

 49                    a[8] = (a[8]+3*x[i])%12;

 50                    break;

 51             case 8:a[7] = (a[7]+3*x[i])%12;

 52                    a[8] = (a[8]+3*x[i])%12;

 53                    a[9] = (a[9]+3*x[i])%12;

 54                    break;

 55             case 9:a[5] = (a[5]+3*x[i])%12;

 56                    a[6] = (a[6]+3*x[i])%12;

 57                    a[8] = (a[8]+3*x[i])%12;

 58                    a[9] = (a[9]+3*x[i])%12;

 59                    break;

 60         }

 61     }

 62     for(i = 1 ; i <= 9 ; i++)

 63     if(a[i]!=0&&a[i]!=12)

 64     {

 65         flag =0;

 66         break;

 67     }

 68     return flag;

 69 }

 70 int main()

 71 {

 72     freopen("clocks.in","r",stdin);

 73     freopen("clocks.out","w",stdout);

 74     int j,k,i[10],kk[10],a[10],b[10],m =0 ;

 75     for(j = 1; j <= 9 ; j++)

 76     {

 77         cin>>a[j];

 78         b[j] = a[j];

 79     }

 80     for(i[1] = 0; i[1] <= 3 ; i[1]++)

 81     {

 82         for(i[2] = 0; i[2] <= 3 ; i[2]++)

 83         {

 84              for(i[3] = 0; i[3] <= 3 ; i[3]++)

 85              {

 86                  for(i[4] = 0; i[4] <= 3 ; i[4]++)

 87                  {

 88                      for(i[5] = 0; i[5] <= 3 ; i[5]++)

 89                      {

 90                          for(i[6] = 0; i[6] <= 3 ; i[6]++)

 91                          {

 92                              for(i[7] = 0; i[7] <= 3 ; i[7]++)

 93                              {

 94                                  for(i[8] = 0; i[8] <= 3 ; i[8]++)

 95                                  {

 96                                      for(i[9] = 0; i[9] <= 3 ; i[9]++)

 97                                      {

 98                                          for(k = 1; k <= 9 ;k++)

 99                                          a[k] = b[k];

100                                          if(pjudge(i,a))

101                                          {

102                                              for(j = 1 ;j <= 9 ; j++)

103                                              {

104                                                  kk[j] = i[j];

105                                              }

106                                              goto to;

107                                          }

108 

109                                      }

110                                  }

111                              }

112                          }

113                      }

114                  }

115              }

116         }

117     }

118     to:

119     {

120         for(j = 1; j <= 9 ; j++)

121         {

122             for(k = 1; k <= kk[j] ; k++)

123             {

124                 if(m!=0)

125                 cout<<" ";

126                 cout<<j;

127                 m++;

128             }

129         }

130     }

131     puts("");

132     fclose(stdin);

133     fclose(stdout);

134     return 0;

135 }

 

你可能感兴趣的:(USACO)