Time limit1000 msMemory limit65536 kBSpecial judgeYesOSLinuxSource
Northeastern Europe 2002, Western Subregion
G - 7 POJ - 3414
problem description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
1.FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
2.DROP(i) empty the pot i to the drain;
3.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)
问题链接:https://vjudge.net/contest/279618#problem/G
问题描述:你会得到两个罐子,分别有A和B升的容量。可执行以下操作:(I)从水龙头中倒满锅I(1≤I≤2);将锅I倒空至排水沟;(i,j)从锅I倒到j锅;在这个操作之后,要么锅j满了(可能在锅I里还剩下一些水),要么锅I是空的(它的所有内容都移到了锅j)。编写一个程序,以找到这些操作的最短序列,这将产生准确的C升水在其中一个罐子。
问题分析:又是经典BFS的隐式搜索题啊,用BFS枚举所有状态。
程序说明:
c++程序如下:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef struct st
{
int ord,d;
int i,j;
st *pre;
};
typedef struct node
{
int va,vb;
};
int a,b,c,vis[210][210];
node s,tp;
st len[500][500],*str[10000];
//va倒入vb
void pour1(node &x)
{
int t=b-x.vb;
if(t>=x.va){
x.vb+=x.va;
x.va=0;
}else{
x.va-=t;
x.vb=b;
}
}
//vb倒入va
void pour2(node &x)
{
int t=a-x.va;
if(t>=x.vb){
x.va+=x.vb;
x.vb=0;
}else{
x.vb-=t;
x.va=a;
}
}
st bfs(queue<node> p)
{
s.va=0,s.vb=0;
vis[0][0]=1;
len[0][0].d=0;
p.push(s);
while(!p.empty())
{
s=p.front();
// printf("%d %d\n",len[s.va][s.vb].ord,len[s.va][s.vb].d);
if(s.va==c||s.vb==c)
return len[s.va][s.vb];
p.pop();
if(s.va>0){
//置空
if(!vis[0][s.vb]){
len[0][s.vb].ord=2,len[0][s.vb].i=1,len[0][s.vb].d=len[s.va][s.vb].d+1;
len[0][s.vb].pre=&len[s.va][s.vb];
tp=s;
tp.va=0,vis[0][tp.vb]=1;
p.push(tp);
}
//加满
if(!vis[a][s.vb]){
len[a][s.vb].ord=1,len[a][s.vb].i=1,len[a][s.vb].d=len[s.va][s.vb].d+1;
len[a][s.vb].pre=&len[s.va][s.vb];
tp=s;
tp.va=a,vis[tp.va][tp.vb]=1;
p.push(tp);
}
//倒进vb中
tp=s;
pour1(tp);
if(!vis[tp.va][tp.vb])
{
len[tp.va][tp.vb].ord=3,len[tp.va][tp.vb].i=1,len[tp.va][tp.vb].d=len[s.va][s.vb].d+1;
len[tp.va][tp.vb].j=2,len[tp.va][tp.vb].pre=&len[s.va][s.vb];
vis[tp.va][tp.vb]=1;
p.push(tp);
}
}
if(s.vb>0){
//置空
if(!vis[s.va][0]){
len[s.va][0].ord=2,len[s.va][0].i=2,len[s.va][0].d=len[s.va][s.vb].d+1;
len[s.va][0].pre=&len[s.va][s.vb];
tp=s;
tp.vb=0,vis[tp.va][tp.vb]=1;
p.push(tp);
}
//加满
if(!vis[s.va][b]){
len[s.va][b].ord=1,len[s.va][b].i=2,len[s.va][b].d=len[s.va][s.vb].d+1;
len[s.va][b].pre=&len[s.va][s.vb];
tp=s;
tp.vb=b,vis[tp.va][tp.vb]=1;
p.push(tp);
}
//倒进va中
tp=s;
pour2(tp);
if(!vis[tp.va][tp.vb])
{
len[tp.va][tp.vb].ord=3,len[tp.va][tp.vb].i=2,len[tp.va][tp.vb].d=len[s.va][s.vb].d+1;
len[tp.va][tp.vb].j=1,len[tp.va][tp.vb].pre=&len[s.va][s.vb];
vis[tp.va][tp.vb]=1;
p.push(tp);
}
}
if(s.va==0)
{
tp=s;
tp.va=a;
if(!vis[a][tp.vb]){
len[a][s.vb].ord=1,len[a][s.vb].i=1,len[a][s.vb].d=len[s.va][s.vb].d+1;
len[a][s.vb].pre=&len[s.va][s.vb];
vis[tp.va][tp.vb]=1;
p.push(tp);
}
}
if(s.vb==0)
{
tp=s;
tp.vb=b;
if(!vis[tp.va][b]){
len[s.va][b].ord=1,len[s.va][b].i=2,len[s.va][b].d=len[s.va][s.vb].d+1;
len[s.va][b].pre=&len[s.va][s.vb];
vis[tp.va][tp.vb]=1;
p.push(tp);
}
}
}
st temp;
temp.ord=-1,temp.d=-1;
return temp;
}
void output(st r)
{
int len=r.d,k,j;
st *t=&r;
str[len]=&r;
for(j=len-1;j>0;j--)
{
str[j]=t->pre;
t=t->pre;
}
printf("%d\n",r.d);
for(k=1;k<=r.d;k++)
{
if(str[k]->ord==1)
printf("FILL(%d)\n",str[k]->i);
else if(str[k]->ord==2)
printf("DROP(%d)\n",str[k]->i);
else
printf("POUR(%d,%d)\n",str[k]->i,str[k]->j);
}
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c))
{
queue<node> p;
memset(vis,0,sizeof(vis));
memset(len,0,sizeof(len));
st r=bfs(p);
if(r.ord==-1&&r.d==-1)
printf("impossible\n");
else
output(r);
}
return 0;
}