WATER BUCKETS
In lethal weapon (I think some movie or other that I haven't seen anyway) some people were asked to get a certain amount of water in one bucket based on two other ones Bucket A and Bucket B which hold in 5 and 7 litres each. I think they were asked (this was to save their lives) to get exactly 3 litres in Bucket B using only these three operations
Fill - completely fill container A
Empty - completely empty container B
Transfer - Transfer contents from A to B untill B is Full or A is empty
this task sounds very hard lets look at a trial set of steps
Step |
Operation |
# of litres in A |
# litres in B |
1 |
Empty |
? |
0 |
2 |
Fill |
5 |
0 |
3 |
Transfer |
0 |
5 |
4 |
Fill |
5 |
5 |
command
5 |
Transfer |
3 |
7 |
6 |
Empty |
3 |
0 |
7 |
Transfer |
0 |
3 |
this is pretty good but can we do it for other amounts?
The answer is yes, It turns out that any amount between 0 and 7 litres can be placed in bucket B you just have to break it down into smaller operations.
(i) If Bucket A is Empty (or unknown) fill it
(ii) If Bucket B is Full empty it
(iii) If Bucket B is not Full or Bucket A is not empty then Transfer
You can see this makes it a lot simpler. I wrote a c++ program that demonstrates this you can download it here.
waterbuckets.cc -The code
waterbuckets.zip - The code and the executatble
|