M-very cola HDU1495 BFS
M-very cola
Time Limit: 1000 MS Memory Limit: 32768KB 64bit IO Format: % I64d & % I64u
Submit Status Practice HDU 1495
Description
You must feel that drinking cola after exercise is a pleasant thing, but seeyou does not think so. Because every time seeyou buy a cola, a ox will ask seeyou to share this bottle of cola, and there must be as much to drink as seeyou. But seeyou has only two cups in his hand. Their capacities are N milliliters and M milliliters respectively. Their capacities are S (S <101) milliliters (just filled with a bottle ), they can pour cola between each other (both have no scale, and S = N + M, 101> S> 0, N> 0, M> 0 ). Smart ACMER, do you mean they can be evenly divided? If you can output the least number of times of cola dumping, if you cannot output "NO ".
Input
Three integers: The size of S cola, N and M are the capacity of the two cups, ending with "0 0 0.
Output
If the values can be evenly divided, output the minimum number of inverted values; otherwise, output "NO ".
Sample Input
7 4 3 4 1 3 0 0 0
Sample Output
NO
3
// Locate the status, and indicate that I am in the (s, n, m) status, and the relationship between pouring water is an edge, BFS // # include
# Include
# Include
# Include
# Include
# Include
# Include
# Define deusing namespace std; const int maxn = 105; const int inf = 200000; # define lson rt <1, l, m # define rson rt <1 | 1, m + 1, r # define For (I, n) for (int I = 0; I <(n); I ++) template
Inline T read (T & x) {char c; while (c = getchar () <= 32); bool OK = false; if (c = '-') OK = true, c = getchar (); for (x = 0; c> 32; c = getchar () x = x * 10 + c-'0 '; if (OK) x =-x; return x;} template
Inline void read _ (T & x, T & y) {read (x); read (y);} template
Inline void write (T x) {if (x <0) putchar ('-'), x =-x; if (x <10) putchar (x + '0'); else write (x/10), putchar (x % 10 + '0');} template
Inline void writeln (T x) {write (x); putchar ('\ n');} // ------- IO template ------ struct node {int s, n, m; node (int a, int B, int c) {s = a; n = B; m = c ;}; int N, M, S; bool OK (node) {int cnt = 0; if (. s = S/2) cnt ++; if (. n = S/2) cnt ++; if (. m = S/2) cnt ++; if (cnt> = 2) return true; return false;} int vis [maxn] [maxn] [maxn]; bool flag = 0; void bfs (int s, int n, int m) {queue
Q; q. push (node (s, n, m); vis [s] [n] [m] = 1; memset (vis, 0, sizeof (vis); while (! Q. empty () {node tmp = q. front (); q. pop (); // printf ("% d \ n", tmp. s, tmp. n, tmp. m); if (OK (tmp) {printf ("% d \ n", vis [tmp. s] [tmp. n] [tmp. m]); flag = false; return;} // s-> n if (tmp. s> 0 & tmp. n
M if (tmp. s> 0 & tmp. m
S if (tmp. n> 0 & tmp. s
M if (tmp. n> 0 & tmp. m
S if (tmp. s
0) {int t = min (tmp. m, S-tmp.s); if (! Vis [tmp. s + t] [tmp. n] [tmp. m-t]) {q. push (node (tmp. s + t, tmp. n, tmp. m-t); vis [tmp. s + t] [tmp. n] [tmp. m-t] = vis [tmp. s] [tmp. n] [tmp. m] + 1 ;}/// m-> n if (tmp. m> 0 & tmp. n