Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11779 | Accepted: 4985 | Special Judge |
Description
You are given two pots, having 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 pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
这个题过程比较简单,就是bfs,但是要考虑到输出和判重的问题让我想了一段时间,我采用的输出方法是在结构体中做一个指针,指向其队列的前一个,当然也可以存其队列前一个的下标.到时候输出的时候顺着向前递归然后输出就好了,当然还需要一个对输出的方式的一个结构体变量.对于判重的问题,我觉得还是用一个二维数组最好,因为其状态比较少,只有100*100个状态,所以可以直接开个数组就好.代码如下:
<span style="font-size:14px;">/************************************************************************* > File Name: Pots.cpp > Author: Zhanghaoran0 > Mail: chiluamnxi@gmail.com > Created Time: 2015年08月01日 星期六 17时24分48秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; struct node{ int A; int B; int method; node *pre; int steps; }q[1000000]; bool flag[110][110]; char output[6][20] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void print_key(node key){ if(key.pre != NULL){ print_key(*(key.pre)); cout << output[key.method] << endl; } } void bfs(int a, int b, int c){ int head = 0, tail = 1; int steps = 0; q[0].A = 0; q[0].B = 0; q[0].steps = 0; q[0].pre = NULL; while(head < tail){ if(q[head].A == c || q[head].B == c){ cout << q[head].steps << endl; print_key(q[head]); return ; } if(!flag[a][q[head].B]){ q[tail].A = a; q[tail].B = q[head].B; q[tail].method = 0; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[a][q[head].B] = true; } if(!flag[q[head].A][b]){ q[tail].A = q[head].A; q[tail].B = b; q[tail].method = 1; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[q[head].A][b] = true; } if(!flag[0][q[head].B]){ q[tail].A = 0; q[tail].B = q[head].B; q[tail].method = 2; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[0][q[head].B] = true; } if(!flag[q[head].A][0]){ q[tail].A = q[head].A; q[tail].B = 0; q[tail].method = 3; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[q[head].A][0] = true; } if(q[head].A + q[head].B > b){ if(!flag[q[head].A - (b - q[head].B)][b]){ q[tail].A = q[head].A - (b - q[head].B); q[tail].B = b; q[tail].method = 4; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[q[head].A - (b - q[head].B)][b] = true; } } else if(q[head].A + q[head].B <= b){ if(!flag[0][q[head].A + q[head].B]){ q[tail].A = 0; q[tail].B = q[head].A + q[head].B; q[tail].method = 4; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[0][q[head].A + q[head].B] = true; } } if(q[head].A + q[head].B > a){ if(!flag[a][q[head].B - (a - q[head].A)]){ q[tail].A = a; q[tail].B = q[head].B - (a - q[head].A); q[tail].method = 5; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[a][q[head].B - (a - q[head].A)] = true; } } else if(q[head].A + q[head].B <= a){ if(!flag[q[head].A + q[head].B][0]){ q[tail].A = q[head].A + q[head].B; q[tail].B = 0; q[tail].method = 5; q[tail].steps = q[head].steps + 1; q[tail ++].pre = &q[head]; flag[q[head].A + q[head].B][0] = true; } } head ++; } cout << "impossible" << endl; } int main(void){ int a, b, c; cin >> a >> b >> c; memset(flag, false, sizeof(flag)); flag[0][0] = true; bfs(a, b, c); return 0; }</span>
这个题WA了好几遍,要怪只能怪自己打字的习惯,导致了空格的多余输出,实在是不该.以后注意.