poj 3279 Pots
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
- 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
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)
倒水的問題,AB兩個容器能不能倒出C所給的數值來;
這個題就是個光搜,可以看成是最短路徑;
一共有6種操作:
倒滿A,倒滿B,清空A,清空B,把A倒入B,把B倒入A;
然後每次的狀態就是A和B的現在有的水的容量;
用一個結構體來記錄每次AB的容量以及到達這個狀態的操作和步數。
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,c;
struct node
{
int v1,v2,step;
string buzhou;
};
bool flag[1000][1000];
node bfs()
{
node k= {0,0,0};
queue<node>Q;
Q.push(k);
flag[0][0]=1;
while(!Q.empty())
{
k=Q.front();
Q.pop();
if(k.v1==c||k.v2==c)
{
return k;
}
node t;
//倒滿1
if(k.v1!=n)
{
t.v1=n;
t.step=k.step+1;
t.buzhou=k.buzhou+'1';
t.v2=k.v2;
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//倒滿2
if(k.v2!=m)
{
t.v2=m;
t.step=k.step+1;
t.buzhou=k.buzhou+'2';
t.v1=k.v1;
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//清空1
if(k.v1!=0)
{
t.v1=0;
t.v2=k.v2;
t.step=k.step+1;
t.buzhou=k.buzhou+'3';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//清空2
if(k.v2!=0)
{
t.v2=0;
t.v1=k.v1;
t.step=k.step+1;
t.buzhou=k.buzhou+'4';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//2匯入1
if(k.v2!=0&&k.v1!=n)
{
t.v2=k.v2-(n-k.v1);
if(t.v2<0) t.v2=0;
t.v1=k.v1+k.v2;
if(t.v1>n) t.v1=n;
t.step=k.step+1;
t.buzhou=k.buzhou+'5';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//1倒入2
if(k.v1!=0&&k.v2!=m)
{
t.v1=k.v1-(m-k.v2);
if(t.v1<0) t.v1=0;
t.v2=k.v2+k.v1;
if(t.v2>m) t.v2=m;
t.step=k.step+1;
t.buzhou=k.buzhou+'6';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
}
k.step=-1;
return k;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&c))
{
memset(flag,0,sizeof(flag));
node ans;
ans=bfs();
if(ans.step>=0)
{
printf("%d\n",ans.step);
int l=ans.buzhou.size();
int i;
for(i=0; i<l; ++i)
{
switch(ans.buzhou[i])
{
case '1':
printf("FILL(1)\n");
break;
case '2':
printf("FILL(2)\n");
break;
case '3':
printf("DROP(1)\n");
break;
case '4':
printf("DROP(2)\n");
break;
case '5':
printf("POUR(2,1)\n");
break;
case '6':
printf("POUR(1,2)\n");
break;
}
}
}
else printf("impossible\n");
}
}