HDU 1495 非常可乐(BFS)

题目链接

题目好理解,汉语的。。。电视上也见过这样的智力游戏,以前看过书,提示是广搜,今天想了一下,思路还是挺清晰的,搜6种情况,如A B C三个杯子,A 到B,A到C,B到A,B到C,C到A,C到B。我实现的很麻烦啊,还好在各种调试,各种DEBUG之后,1Y。

  1 #include <stdio.h>

  2 #include <stdlib.h>

  3 #include <string.h>

  4 int p[102][102],o1[10000],o2[10000],o3[10000];

  5 int main()

  6 {

  7     int i,j,s,n,m,start,end,num,z;

  8     while(scanf("%d%d%d",&s,&n,&m)!=EOF)

  9     {

 10         start = end = 1;

 11         num = 1;

 12         z = 0;

 13         memset(p,0,sizeof(p));

 14         if(s == 0&&n == 0&&m == 0)

 15             break;

 16         o1[1] = s;

 17         o2[1] = 0;

 18         o3[1] = 0;

 19         p[s][0] = 1;

 20         while(start<=end)

 21         {

 22             j = 1;

 23             for(i = start; i <= end; i ++)

 24             {

 25                 if(o1[i] != 0)

 26                 {

 27                     if(o1[i] >= n - o2[i])

 28                     {

 29                         if(p[o1[i]-n+o2[i]][n]==0)

 30                         {

 31                             o1[end+j] = o1[i] - (n - o2[i]);

 32                             o2[end+j] = n;

 33                             o3[end+j] = o3[i];

 34                             p[o1[i]-n+o2[i]][n] = 1;

 35                             j ++;

 36                         }

 37                     }

 38                     else

 39                     {

 40                         if(p[0][o1[i]+o2[i]]==0)

 41                         {

 42                             o1[end+j] = 0;

 43                             o2[end+j] = o1[i] +o2[i];

 44                             o3[end+j] = o3[i];

 45                             p[0][o1[i]+o2[i]] = 1;

 46                             j ++;

 47                         }

 48                     }

 49                     if(o1[i] >= m - o3[i])

 50                     {

 51                         if(p[o1[i]-m+o3[i]][o2[i]]==0)

 52                         {

 53                             o1[end+j] = o1[i] - (m - o3[i]);

 54                             o2[end+j] = o2[i];

 55                             o3[end+j] = m;

 56                             p[o1[i]-m+o3[i]][o2[i]] = 1;

 57                             j ++;

 58                         }

 59                     }

 60                     else

 61                     {

 62                         if(p[0][o2[i]]==0)

 63                         {

 64                             o1[end+j] = 0;

 65                             o2[end+j] = o2[i];

 66                             o3[end+j] = o3[i]+o1[i];

 67                             p[0][o2[i]] = 1;

 68                             j ++;

 69                         }

 70                     }

 71                 }

 72                 if(o2[i] != 0)

 73                 {

 74                     if(p[o1[i]+o2[i]][0] == 0)

 75                     {

 76                         o1[end+j] = o1[i]+o2[i];

 77                         o2[end+j] = 0;

 78                         o3[end+j] = o3[i];

 79                         p[o1[i]+o2[i]][0] = 1;

 80                         j ++;

 81                     }

 82                     if(o2[i] >= m - o3[i])

 83                     {

 84                         if(p[o1[i]][o2[i]-m+o3[i]] == 0)

 85                         {

 86                             o1[end+j] = o1[i];

 87                             o2[end+j] = o2[i] - m + o3[i];

 88                             o3[end+j] = m;

 89                             p[o1[i]][o2[i]-m+o3[i]] = 1;

 90                             j ++;

 91                         }

 92                     }

 93                     else

 94                     {

 95                         if(p[o1[i]][0]==0)

 96                         {

 97                             o1[end+j] = o1[i];

 98                             o2[end+j] = 0;

 99                             o3[end+j] = o2[i] + o3[i];

100                             p[o1[i]][0] = 1;

101                             j ++;

102                         }

103                     }

104                 }

105                 if(o3[i] != 0)

106                 {

107                     if(p[o1[i]+o3[i]][o2[i]] == 0)

108                     {

109                         o1[end+j] = o1[i]+o3[i];

110                         o2[end+j] = o2[i];

111                         o3[end+j] = 0;

112                         p[o1[i]+o3[i]][o2[i]] = 1;

113                         j ++;

114                     }

115                     if(o3[i] >= n - o2[i])

116                     {

117                         if(p[o1[i]][n] == 0)

118                         {

119                             o1[end+j] = o1[i];

120                             o2[end+j] = n;

121                             o3[end+j] = o2[i] - n + o3[i];

122                             p[o1[i]][n] = 1;

123                             j ++;

124                         }

125                     }

126                     else

127                     {

128                         if(p[o1[i]][o2[i]+o3[i]] == 0)

129                         {

130                             o1[end+j] = o1[i];

131                             o2[end+j] = o2[i] + o3[i];

132                             o3[end+j] = 0;

133                             p[o1[i]][o2[i]+o3[i]] = 1;

134                             j ++;

135                         }

136                     }

137                 }

138             }

139             start = end + 1;

140             end = end + j - 1;

141             for(i = start;i <= end;i ++)

142             {

143                 if(o1[i] == o3[i]&&o2[i] == 0)

144                 {

145                     z = 1;

146                     break;

147                 }

148                 if(o2[i] == o3[i]&&o1[i] == 0)

149                 {

150                     z = 1;

151                     break;

152                 }

153                 if(o1[i] == o2[i]&&o3[i] == 0)

154                 {

155                     z = 1;

156                     break;

157                 }

158 

159             }

160             if(z) break;

161             num ++;

162         }

163         if(z)

164             printf("%d\n",num);

165         else

166             printf("NO\n");

167     }

168     return 0;

169 }

 

 

你可能感兴趣的:(HDU)