Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 9985 | Accepted: 4192 | 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.
给你两个锅,各自的容积为A公升和B公升。下面是可以执行的动作:
1.FILL(i) 向锅i (1 ≤ i ≤ 2) 里面灌水
2.DROP(i) 把锅里的水倒进下水道
3.POUR(i,j) 把i里面的水倒到j里面;直到把j倒满,或者把i倒空。
写一个程序找到一个使用最少的操作,能将任意一个容器的水达到C升。
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).
只有一行,输入A,B,C。范围都在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’.
第一行输出,得操作多少次K,接下来K行来描述,如果有几种完成方式,输出任意一种。如果没办法完成,输出‘impossible’。
Sample Input
3 5 4
Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
Source
#include <stdio.h>
#include <string.h>
struct Pots{
int volume[2];
int step;
int pre;
int opt;//0->a_fill;1->b_fill;2->a_pour_b;3->b_pour_a;4->a_drop;5->b_drop;
};
int a, b, c;
Pots fifo[33333];
int min_num(int a, int b);
void Fill(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void Drop(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void Pour(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void output(Pots *tmp);
int main(void){
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//
int vis[111][111];
int head = 0, tail = 1;
int i;
scanf("%d%d%d", &a, &b, &c);
memset(vis, 0, sizeof(vis));
fifo[head].step = 0;
fifo[head].pre = 0;
fifo[head].volume[0] = 0;
fifo[head].volume[1] = 0;
vis[0][0] = 1;
while(head < tail){
Pots tmp = fifo[head];
if (tmp.volume[0] == c || tmp.volume[1] == c){
output(&tmp);
return 0;
}
for (i = 0;i < 2;i ++) {
//printf("head = %d, volume[0]=%d, volume[1]=%d\n", head, tmp.volume[0], tmp.volume[1]);
Fill(&tmp, head, &tail, i, vis);
Drop(&tmp, head, &tail, i, vis);
Pour(&tmp, head, &tail, i, vis);
}
head ++;
}
printf("impossible\n");
}
void output(Pots *tmp){
int cnt = 0;
int loc[1111];
int i;
while(tmp->step){
loc[cnt++] = tmp->opt;
tmp = &fifo[tmp->pre];
}
printf("%d\n", cnt);
for (i = cnt - 1;i >= 0;i --){
switch (loc[i]){
case 0: printf("FILL(1)\n");break;
case 1: printf("FILL(2)\n");break;
case 2: printf("POUR(1,2)\n");break;
case 3: printf("POUR(2,1)\n");break;
case 4: printf("DROP(1)\n");break;
case 5: printf("DROP(2)\n");break;
}
}
}
void Fill(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
if (!i){
if (tmp->volume[0] != a && (!vis[a][tmp->volume[1]])){
fifo[*tail].volume[0] = a;
fifo[*tail].volume[1] = tmp->volume[1];
fifo[*tail].opt = 0;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[a][fifo[*tail].volume[1]] = 1;
//printf("fir : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}
}else{
if (tmp->volume[1] != b && (!vis[tmp->volume[0]][b])){
fifo[*tail].volume[1] = b;
fifo[*tail].volume[0] = tmp->volume[0];
fifo[*tail].opt = 1;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[fifo[*tail].volume[0]][b] = 1;
//printf("sed : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}
}
return;
}
void Drop(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
int temp_min;
if (!i){
temp_min = min_num(tmp->volume[0], b - tmp->volume[1]);
if (tmp->volume[0] != 0 && tmp->volume[1] != b && (!vis[tmp->volume[0] - temp_min][tmp->volume[1] + temp_min])){
fifo[*tail].volume[0] = tmp->volume[0] - temp_min;
fifo[*tail].volume[1] = tmp->volume[1] + temp_min;
fifo[*tail].opt = 2;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
//printf("thr : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}
}else{
temp_min = min_num(a - tmp->volume[0], tmp->volume[1]);
if (tmp->volume[0] != a && tmp->volume[1] != 0 && (!vis[tmp->volume[0] + temp_min][tmp->volume[1] - temp_min])){
fifo[*tail].volume[0] = tmp->volume[0] + temp_min;
fifo[*tail].volume[1] = tmp->volume[1] - temp_min;
fifo[*tail].opt = 3;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
//printf("for : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}
}
return;
}
void Pour(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
if (!i)
if (tmp->volume[0] != 0 && (!vis[0][tmp->volume[1]])){
fifo[*tail].volume[0] = 0;
fifo[*tail].volume[1] = tmp->volume[1];
fifo[*tail].opt = 4;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
//printf("fif : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}else if (tmp->volume[1] != 0 && (!vis[tmp->volume[1]][0])){
fifo[*tail].volume[1] = 0;
fifo[*tail].volume[0] = tmp->volume[0];
fifo[*tail].opt = 5;
fifo[*tail].step = tmp->step + 1;
fifo[*tail].pre = head;
vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
//printf("six : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
(*tail) ++;
}
}
int min_num(int a, int b){
return a < b ? a : b;
}