Problem link :HDU1495 very Coke .
Test instructions briefly : Divide the liquid problem. Input s, n and M three numbers, respectively, for Coke and 2 cups, three containers can pour each other, ask if you can put the cola in the s in Split, can output the smallest number of cups, you can not output No.
problem Analysis : At the beginning, all cola in S, 2 cups n and M are empty. Process, you can pour coke from any container into another container, fill the target container, or empty the source container. Because the container has no scale, this is the only way. In this process, if a container appears empty, the other 2 containers of Coke are the same success. If the Coke's capacity is odd at first, it is impossible to split the coke. Between the container Coke pour to pour, each time there are 6 kinds of inverted method, the 6 kinds of inverted method can be tempted. The minimum number of pours, so it can be implemented with BFS.
Program description : The searched state does not need to search again, with the array notvist[][][] to mark the status of the search.
The AC C + + language program is as follows:
/* HDU1495 very cola * * #include <iostream> #include <queue> #include <cstring> #include <cstdio>using namespace Std;const int maxn = 100;int s, N, m, s2;bool notvist[maxn+1][maxn+1][maxn+1];struct node {int s, n, M, leve l;}; int BFs () {if (s% 2 = = 1) return-1; Queue<node> Q; s2 = S/2; Memset (Notvist, True, sizeof (notvist)); Node F; F.s = s; F.N = 0; F.M = 0; f.level=0; Q.push (f); NOTVIST[F.S][F.N][F.M] = false; while (!q.empty ()) {f = Q.front (); Q.pop (); if (F.s = = F.N && F.s = = s2) | | (F.s = = f.m && F.s = = s2) | | (f.m = = F.N && f.m = = s2)) return f.level; Node V; S-to N if (f.s && N-F.N > 0) {if (F.s > N-F.N) {//s > N remaining capacity V.s = F.s-(N-F.N); V.N = n; V.M = F.M; } else {//s <= n remaining capacity v.s = 0; V.N = F.N + F.s; V.M = F.M; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}//S--and M if (f.s && m-f.m > 0) {if (F.s > M-F.M) {//S Remaining capacity of > M v.s = F.s-(M-F.M); V.N = F.N; V.M = m; } else {//s <= m of remaining capacity V.s = 0; V.N = F.N; V.M = f.m + F.s; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}//N--S if (F.N && s-f.s > 0) {if (F.N > S-f.s) {//N The remaining capacity of > s v.s = s; V.N = F.N-(S-F.S); V.M = F.M; } else {//n <= S of remaining capacity V.s = F.s + F.N; V.N = 0; V.M = F.M; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}//N-and M if (F.N && m-f.m > 0) {if (F.N > M-F.M) {//N The remaining capacity of > M v.s = f.s; V.N = F.N-(M-F.M); V.M = m; } else {//n <= m of remaining capacity v.s = F.s; V.N = 0; V.M = f.m + F.N; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}//M--s if (f.m && s-f.s > 0) {if (f.m > S-f.s) {//M The remaining capacity of > s v.s = s; V.N = F.N; V.M = f.m-(S-F.S); } else {//M <= S's remaining capacity V.s = F.s + f.m; V.N = F.N; V.M = 0; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}//M-and N if (f.m && N-F.N > 0) {if (f.m > N-F.N) {//M residual capacity of > N v.s = f.s; V.N = n; V.M = f.m-(N-F.N); } else {//M <= n remaining capacity v.s = F.s; V.N = F.N + f.m; V.M = 0; } if (notvist[v.s][v.n][v.m]) {NOTVIST[V.S][V.N][V.M] = false; V.level = F.level + 1; Q.push (v); }}} return-1;} int main () {while (scanf ("%d%d%d", &s, &n, &m) ! = EOF) {if (s = = 0 && n = = 0 && m = = 0) break; int ans = BFS (); if (ans < 0) printf ("no\n"); else printf ("%d\n", ans); } return 0;}
HDU1495 very coke.