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)
题意简述:
给出两个壶的容量A和B, 一个目标水量C,对A、B可以有3种操作,求最少经过几步操作能够在某个壶中得到目标水量C。输入A、B和C,输入最少操作数和操作过程。
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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).
问题分析:
把A和B壶中水量作为状态,初始状态为<0,0>,每个操作都是状态变化的过程。因为有2个壶,所以总共有6种操作。使用BFS搜索来找到最少的操作步数。同时需要考虑操作的条件,以减少操作来加快程序运行速度。
C++版本一
BFS
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int a,b,c,ans;
int vis[110][110];
struct node{
int x,y;
int cnt;
int step[1000][3];
}front1,start,temp;
void bfs(){
if(a<c&&b<c){
cout << "impossible" << endl;
return;
}
queue <node>Q;
memset(vis,0,sizeof(vis));
while(!Q.empty())
Q.pop();
start.x=0;
start.y=0;
start.cnt=0;
memset(start.step,0,sizeof(start.step));
vis[start.x][start.y]=1;
Q.push(start);
while(!Q.empty()){
front1=Q.front();
Q.pop();
//cout << front1.x<<front1.y <<front1.step[front1.cnt][0]<< endl;
if(front1.x==c||front1.y==c){
cout <<front1.cnt<< endl;
for(int i=1;i<=front1.cnt;i++){
if(front1.step[i][0]==1)
cout << "FILL("<<front1.step[i][1]<<")" << endl;
if(front1.step[i][0]==2)
cout << "DROP("<<front1.step[i][1]<<")" << endl;
if(front1.step[i][0]==3)
cout << "POUR("<<front1.step[i][1]<<","<<front1.step[i][2]<<")" << endl;
}
return ;
}
//FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
if(front1.x<a){
temp=front1;
temp.x=a;
if(vis[temp.x][temp.y]==0){
temp.cnt++;
temp.step[temp.cnt][0]=1;
temp.step[temp.cnt][1]=1;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
if(front1.y<b){
temp=front1;
temp.y=b;
if(vis[temp.x][temp.y]==0){
temp.cnt++;
temp.step[temp.cnt][0]=1;
temp.step[temp.cnt][1]=2;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
//DROP(i) empty the pot i to the drain;
if(front1.x>0){
temp=front1;
temp.x=0;
if(vis[temp.x][temp.y]==0){
temp.cnt++;
temp.step[temp.cnt][0]=2;
temp.step[temp.cnt][1]=1;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
if(front1.y>0){
temp=front1;
temp.y=0;
if(vis[temp.x][temp.y]==0){
temp.cnt++;
temp.step[temp.cnt][0]=2;
temp.step[temp.cnt][1]=2;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
//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).
if(front1.x>0&&front1.y<b){
temp=front1;
if(front1.x+front1.y-b<0){
temp.x=0;
temp.y=front1.x+front1.y;
}
else{
temp.x=front1.x+front1.y-b;
temp.y=b;
}
if(vis[temp.x][temp.y]==0){
temp.cnt=front1.cnt+1;
temp.step[temp.cnt][0]=3;
temp.step[temp.cnt][1]=1;
temp.step[temp.cnt][2]=2;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
if(front1.x<a&&front1.y>0){
temp=front1;
if(front1.x+front1.y-a<0){
temp.x=front1.x+front1.y;
temp.y=0;
}
else{
temp.x=a;
temp.y=front1.x+front1.y-a;
}
if(vis[temp.x][temp.y]==0){
temp.cnt=front1.cnt+1;
temp.step[temp.cnt][0]=3;
temp.step[temp.cnt][1]=2;
temp.step[temp.cnt][2]=1;
vis[temp.x][temp.y]=1;
Q.push(temp);
}
}
}
cout << "impossible" << endl;
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
bfs();
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
BFS
/* POJ3414 Pots */
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 100;
int a, b, c;
bool notvist[MAXN+1][MAXN+1];
struct node {
int a, b, level;
char path[MAXN+1];
int plen;
};
string path[] = {
"FILL(1)"
,"FILL(2)"
,"DROP(1)"
,"DROP(2)"
,"POUR(1,2)"
,"POUR(2,1)"
};
void output_result(int lvl, char p[], int n)
{
cout << lvl << endl;
for(int i=0; i<n; i++)
cout << path[(int)p[i]] << endl;
}
void bfs()
{
queue<node> q;
memset(notvist, true, sizeof(notvist));
node f;
f.a = 0;
f.b = 0;
f.level = 0;
memset(f.path, 0, sizeof(f.path));
f.plen = 0;
q.push(f);
notvist[f.a][f.b] = false;
while(!q.empty()) {
f = q.front();
q.pop();
if(f.a == c || f.b == c) {
output_result(f.level, f.path, f.plen);
return;
}
node v;
v = f;
v.level++;
v.plen++;
// FILL(a)
if(a - f.a > 0) {
v.a = a;
v.b = f.b;
if(notvist[v.a][v.b]) {
v.path[f.plen] = 0;
q.push(v);
notvist[v.a][v.b] = false;
}
}
// FILL(b)
if(b - f.b > 0) {
v.a = f.a;
v.b = b;
if(notvist[v.a][v.b]) {
v.path[f.plen] = 1;
q.push(v);
notvist[v.a][v.b] = false;
}
}
// DROP(a)
if(f.a) {
v.a = 0;
v.b = f.b;
if(notvist[v.a][v.b]) {
v.path[f.plen] = 2;
q.push(v);
notvist[v.a][v.b] = false;
}
}
// DROP(b)
if(f.b) {
v.a = f.a;
v.b = 0;
if(notvist[v.a][v.b]) {
v.path[f.plen] = 3;
q.push(v);
notvist[v.a][v.b] = false;
}
}
// POUR(a,b)
if(f.a && (f.b < b)) {
if(f.a > (b - f.b)) {
v.a = f.a -(b - f.b);
v.b = b;
} else {
v.a = 0;
v.b = f.b + f.a;
}
if(notvist[v.a][v.b]) {
v.path[f.plen] = 4;
q.push(v);
notvist[v.a][v.b] = false;
}
}
// POUR(b,a)
if(f.b && (f.a < a)) {
if(f.b > (a - f.a)) {
v.a = a;
v.b = f.b -(a - f.a);
} else {
v.a = f.a + f.b;
v.b = 0;
}
if(notvist[v.a][v.b]) {
v.path[f.plen] = 5;
q.push(v);
notvist[v.a][v.b] = false;
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}
C++版本三
BFS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
using namespace std;
int n,m,c;
struct node
{
string s;
int a,b;
int step;
};
int v[111][111];
void bfs()
{
memset(v,0,sizeof(v));
queue<node>q;
while(!q.empty())q.pop();
struct node l,k;
l.a=0;
l.b=0;
l.s+='0';
l.step=0;
q.push(l);
int top=0;
while(!q.empty())
{
k=q.front();
q.pop();
if(k.a==c||k.b==c)
{
printf("%d\n",k.step);
struct node t;
t=k;
//cout<<k.s<<endl;
for(int i=0;k.s[i]!='\0';i++)
{
if(k.s[i]=='1')
{
printf("FILL(1)\n");
}
if(k.s[i]=='2')
{
printf("FILL(2)\n");
}
if(k.s[i]=='3')
{
printf("DROP(1)\n");
}
if(k.s[i]=='4')
{
printf("DROP(2)\n");
}
if(k.s[i]=='5')
{
printf("POUR(1,2)\n");
}
if(k.s[i]=='6')
{
printf("POUR(2,1)\n");
}
}
return ;
}
if(k.a<n)
{
struct node t;
t.a=n;
t.b=k.b;
t.s=k.s+'1';//存操作
t.step=k.step+1;
if(!v[t.a][t.b])//标记状态
{
q.push(t);
v[t.a][t.b]=1;
}
}
if(k.b<m)
{
struct node t;
t.a=k.a;
t.b=m;
t.s=k.s+'2';
t.step=k.step+1;
if(!v[t.a][t.b])
{
q.push(t);
v[t.a][t.b]=1;
}
}
if(k.a!=0)
{
struct node t;
t.a=0;
t.b=k.b;
t.s=k.s+'3';
t.step=k.step+1;
if(!v[t.a][t.b])
{
q.push(t);
v[t.a][t.b]=1;
}
}
if(k.b!=0)
{
struct node t;
t.a=k.a;
t.b=0;
t.s=k.s+'4';
t.step=k.step+1;
if(!v[t.a][t.b])
{
q.push(t);
v[t.a][t.b]=1;
}
}
if(k.a!=0 && k.a<=n && k.b<m)
{
struct node t;
int g=m-k.b;
if(g<k.a)
{
t.a=k.a-g;
}
else
{
t.a=0;
}
if(g>k.a)
{
t.b=k.b+k.a;
}
else
{
t.b=m;
}
t.s=k.s+'5';
t.step=k.step+1;
if(!v[t.a][t.b])
{
q.push(t);
v[t.a][t.b]=1;
}
}
if(k.b!=0 && k.b<=m && k.a<n)
{
struct node t;
int g=n-k.a;
if(g<k.b)
{
t.b=k.b-g;
}
else
{
t.b=0;
}
if(g>k.b)
{
t.a=k.b+k.a;
}
else
{
t.a=n;
}
t.s=k.s+'6';
t.step=k.step+1;
if(!v[t.a][t.b])
{
q.push(t);
v[t.a][t.b]=1;
}
}
}
printf("impossible\n");
return ;
}
int main()
{
scanf("%d%d%d",&n,&m,&c);
bfs();
return 0;
C++版本四
BFS
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 101+5;//杯子中水的水量的取值范围为0~100
///定义6个动作,使用数组下标0~5存储
const char* action[] = {"FILL(1)","FILL(2)","POUR(1,2)",
"POUR(2,1)","DROP(1)","DROP(2)"};
int a,b,c;
struct node{
int anum,bnum;//记录节点,A中的水量,B中的水量
int index;//记录action数组的下标,父节点是通过什么动作到达当前节点
int deep;//记录深度
int par;//记录父节点
};
///为防止访问相同的结点,使用二维数组visit[i][j],
///表示A中水量为i,B中水量为j时的状态是否被访问过。
bool visit[maxn][maxn];
///因为重复的结点不会访问,所以最多的结点数为maxn*maxn
node queue[maxn*maxn];
int ans[maxn*maxn];
void change(int na,int nb,int &ta,int &tb,int i)
{///"FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)"
if(i == 0)//"FILL(1)"
{
ta = a;tb = nb;
}
else if(i==1)
{
ta = na;tb = b;
}
else if(i==2)
{
if(na>=(b-nb))///把b倒满
{
ta = na - (b-nb);tb = b;
}
else{///把a倒空
ta = 0;tb = na+nb;
}
}
else if(i==3)
{
if(nb>=(a-na))///把a倒满
{
tb = nb - (a-na);ta = a;
}
else{///把b倒空
tb = 0;ta = nb+na;
}
}
else if(i==4)
{
ta = 0;tb = nb;
}
else{
tb = 0;ta = na;
}
}
///六入口的bfs
bool bfs()
{
///定义状态
queue[0].anum = 0;
queue[0].bnum = 0;
queue[0].deep = 0;
queue[0].index = -1;
queue[0].par = -1;
visit[0][0] = true;
int head = 0,rear = 1;
while(head<rear)
{
node u = queue[head];
if((u.anum==c)||(u.bnum==c))
{///得到目标状态
int dept = u.deep;
cout<<dept<<endl;
for(int i=u.deep;i>0;i--)
{
if(u.par == -1) break;
ans[i] = u.index;
u = queue[u.par];
}
for(int i=1;i<=dept;i++)
cout<<action[ans[i]]<<endl;
return true;
}
for(int i=0;i<6;i++)///遍历6个入口
{
int ta,tb;
change(u.anum,u.bnum,ta,tb,i);
if(!visit[ta][tb])
{///当前状态没有被访问过
queue[rear].anum = ta;
queue[rear].bnum = tb;
queue[rear].deep = u.deep+1;
queue[rear].index = i;
queue[rear].par = head;
visit[ta][tb] = true;
rear++;
}
}
head++;
}
return false;
}
int main()
{
while(cin>>a>>b>>c)
{
memset(visit,false,sizeof(visit));
if(!bfs())
cout<<"impossible"<<endl;
}
return 0;
}