poj 3414 Pots (bfs+路徑記錄)
Pots
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 10071
Accepted: 4237
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)
思路:一共有6種操作:把A中水倒掉,把A加滿,把B裡的水倒入A中;B和A類似。
罐子最大容積為100,設一個常量N=100, 開一個二維數組記錄狀態變化的值。
1、從水龍頭往A裡加水t,記錄-t,
2、從水龍頭往B裡加水t,記錄-t-N,
3、從B裡面往A加水t,記錄t
4、從A裡面往B加水t,記錄N+t
5、把A裡水倒掉,記錄2*N+t,(A原有水t)
6、把B裡水倒掉,記錄3*N+t,(B原有水t)
#include
#include
#include