Pots
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10149 |
|
Accepted: 4275 |
|
Special Judge |
Description
You are given the pots of the volume of A and B liters respectively. The following operations can be performed:
- Fill (i) Fill the pot i (1≤ i ≤ 2) from the tap;
- DROP (i) empty the pot I to the drain;
- Pour (i,j) pour from pot i to pot J; After this operation either the potJ was full (and there may be some water left in the pot I), or the pot i was empty (and all its contents has been moved to the pot J).
Write a program to find the shortest possible sequence of these operations that would yield exactlyC liters of W Ater in one of the pots.
Input
On the first and only line is the numbers A, B, andC. These is all integers in the range from 1 to + C≤max (A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The following K lines must each describe one operation. If There is several sequences of minimal length, output any one of them. If the desired result can ' t is achieved, the first and only line of the file must contain the word 'impossible'.
Sample Input
3 5 4
Sample Output
6FILL (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1)
There is a A, b two bottles, get the volume of C liquid, six kinds of operation
Fill 1. Fill 2, pour 1. Pour 2, pour from 1 to 2, 2 pour to 1.
The
records the path. That is, who has reached the current state
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;struct node{int A, B, p Re; Char s[20];} P[1000000], x; int flag[20000], low, top, K; char str[6][20] = {"Fill (1)", "Fill (2)", "Drop (1)", "Drop (2)", "pour", " Pour (2,1) "};int pre[1000000"; int main () {int A, B, C, Sum, I, J; k = 0; memset (flag,0,sizeof (flag)); scanf ("%d%d%d", &a, &b, &c); FLAG[A*100+B] = 1; Low = top = 0; P[TOP].A = 0; p[top].b = 0; P[top++].pre =-1; while (Low < top) {if (p[low].a = = c | | p[low].b = = c) break; if (P[low].a < a) {p[top].a = A; p[top].b = p[low].b; if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (P[top].s,str[0]); P[top++].pre = low; }} if (P[low].b < b) {p[top].a = p[low].a; p[top].b = b; if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (p[top].s,str[1]); P[top++].pre = low; }} if (P[low].a > 0) {p[top].a = 0; p[top].b = p[low].b; if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (p[top].s,str[2]); P[top++].pre = low; }} if (p[low].b > 0) {p[top].a = p[low].a; p[top].b = 0; if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (P[top].s,str[3]); P[top++].pre = low; }} if (P[low].a > 0 && p[low].b < b) {sum = p[low].a + p[low].b; if (sum <= b) {p[top].a = 0; p[top].b = sum; } else {p[top].a = sum-b; p[top].b = b; } if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (P[top].s,str[4]); P[top++].pre = low; }} if (P[low].a < a && p[low].b > 0) {sum = p[low].a + p[low].b; if (sum <= a) {p[top].a = sum; p[top].b = 0; } else {p[top].a = A; p[top].b = sum-a; } if (!flag[p[top].a*100+p[top].b]) {flag[p[top].a*100+p[top].b] = 1; strcpy (P[top].s,str[5]); P[top++].pre = low; }} low++; } if (low = = top) printf ("impossible\n"); else {j = 0; for (i = low; I! = 0; i = p[i].pre) {pre[j++] = i; } printf ("%d\n", j); for (J-= 1; J >= 0; j--) printf ("%s\n", p[Pre[j]].s); } return 0;}
Copyright NOTICE: Reprint Please specify source: Http://blog.csdn.net/winddreams
Poj3414--pots (BFS, record Path)