题目描述
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)
解题思路
求最短路径的问题,用宽度优先搜索。搜索的方向是不同的操作,这里有六种操作, FILL(2)、POUR(2,1)、 DROP(2)
FILL(1)、POUR(1,2)、DROP(1)。
1、这道题的状态是两只水桶中水的重量,因此,需要使用两个数字来描述状态。宽度优先搜素时候,我们需要记录哪些节点是经历过的,由于需要两个数字来描述状态,那么开一个一维数组mark[]记录哪些节点是经历过的就不现实了。在这里,我使用了一个转换,选了一个单射函数,将二维的两个桶的水的重量映射到一维的数组上。x代表第一个桶的重量,y代表第二个桶的重量,我们使用x*101+y+1这个函数,将二维的状态映射到一维的数组上。这样就可以开一个一维的数组来记录那些节点是经历过的。
2、这道题还需要打印出来路径,我们在宽搜的时候记录每个节点的父亲节点。结束搜索的时候只要倒着往回找就能找到路径了。
AC代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int A,B,C;
int Pre[10202];
int Action[10202];
int Mark[10202];
char str[6][10]= {"FILL(1)", "FILL(2)", "DROP(1)","DROP(2)","POUR(1,2)", "POUR(2,1)"};
struct node
{
int a,b,step;
};
struct Res
{
int result,p;
};
Res bfs()
{
int add_a,add_b;
Res Result;
queue<node> que;
node new_node,add_node;
add_node.a=0;
add_node.b=0;
add_node.step = 0;
que.push(add_node);
Mark[add_node.a*101+add_node.b+1] = 1;
while(!que.empty())
{
new_node = que.front();
que.pop();
if(new_node.a==C||new_node.b==C)
{
// cout<<new_node.a<<" "<<new_node.b<<" "<<new_node.a*101+new_node.b+1<<endl;
Result.p = new_node.step;
Result.result = new_node.a*101+new_node.b+1;
return Result;
}
for(int i = 0;i<6;i++)
{
if(i==0)//FILL(1)
{
add_a = A;
add_b = new_node.b;
if(new_node.a>=A)
{
// cout<<"**";
continue;
}
// cout<<"--"<<add_a<<" "<<add_b<<" "<<add_a*101+add_b+1<<" "<<Mark[add_a*101+add_b+1]<<" ";
}
if(i==1)//FILL(2)
{
add_a = new_node.a;
add_b = B;
if(new_node.b>=B) continue;
}
if(i==2)//DROP(1)
{
add_a = 0;
add_b = new_node.b;
if(new_node.a==0) continue;
}
if(i==3)//DROP(2)
{
add_a = new_node.a;
add_b = 0;
if(new_node.b==0) continue;
}
if(i==4)//POUR(1,2)
{
if(new_node.a==0||new_node.b==B) continue;
int cha_b = B-new_node.b;
if(cha_b>=new_node.a)
{
add_a = 0;
add_b = new_node.b+new_node.a;
}
else
{
add_a=new_node.a-cha_b;
add_b = B;
}
}
if(i==5)//POUR(2,1)
{
if(new_node.a == A||new_node.b == 0) continue;
int cha_a = A-new_node.a;
if(cha_a>=new_node.b)
{
add_a = new_node.a + new_node.b;
add_b = 0;
}
else
{
add_a = A;
add_b = new_node.b-cha_a;
}
}
if(Mark[add_a*101+add_b+1]==0)
{
// if(i==0) cout<<"++";
// cout<< str[i]<<endl;
add_node.a = add_a;
add_node.b = add_b;
add_node.step = new_node.step+1;
Mark[add_a*101+add_b+1] = 1;
Action[add_a*101+add_b+1] = i;
Pre[add_a*101+add_b+1] = new_node.a*101+new_node.b+1;
que.push(add_node);
}
}
}
Result.p = -1;
Result.result = -1;
return Result;
}
int main()
{
cin>>A>>B>>C;
int count[10202];
// for(int i = 0;i<6;i++) cout<<str[i]<<endl;
// cout<<A<<B<<C;
memset(Mark,0,sizeof(Mark));
// cout<<Mark[1213]<<endl;
// cout<<
Res Result;
int p, result;
Result = bfs();
p = Result.p;
result = Result.result;
// cout<<Result.result<<"**"<<Result.p<<endl;
if(p==-1) cout<<"impossible"<<endl;
else
{
cout<<p<<endl;
// cout<<result<<endl;
int index = 0;
while(result!=1)
{
count[index] = Action[result];
result = Pre[result];
index++;
}
index--;
for(int i = index;i>=0;i--) cout<<str[count[i]]<<endl;
}
}