题目描述:
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.
输入:
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).
输出:
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’.
样例输入:
3 5 4
样例输出:
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
额,一道很是麻烦的广搜题,将所有可能的数字一个一个的推进去,直到找到正确结果
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int a,b,c;
int flag[200][200];
int d[200][200];
#include<queue>
typedef pair<int,int> P;
int biao[200][200];
P father[200][200];
#include<stack>
stack<P> feng;
P first;
bool bfs()
{
memset(d,0,sizeof(d));
queue<P> que;
que.push(P(0,0));
flag[0][0]=1;
d[0][0]=0;
while(!que.empty())
{
P p=que.front();
que.pop();
if(p.first==c||p.second==c)
{
printf("%d\n",d[p.first][p.second]);
first=p;
feng.push(P(p.first,p.second));
return true;
}
int aa=p.first;
int bb=p.second;
int adaoba=0;
int adaobb=aa+bb;
if(aa+bb>b)
{
adaobb=b;
adaoba=(aa+bb)-b;
}
int bdaoab=0;
int bdaoaa=aa+bb;
if(bdaoaa>a)
{
bdaoaa=a;
bdaoab=(aa+bb)-a;
}
if(flag[adaoba][adaobb]==0)
{
biao[adaoba][adaobb]=5;
father[adaoba][adaobb].first=p.first;
father[adaoba][adaobb].second=p.second;
flag[adaoba][adaobb]=1;
que.push(P(adaoba,adaobb));
d[adaoba][adaobb]=d[aa][bb]+1;
}
if(flag[bdaoaa][bdaoab]==0)
{
biao[bdaoaa][bdaoab]=6;
father[bdaoaa][bdaoab].first=p.first;
father[bdaoaa][bdaoab].second=p.second;
flag[bdaoaa][bdaoab]=1;
que.push(P(bdaoaa,bdaoab));
d[bdaoaa][bdaoab]=d[aa][bb]+1;
}
if(flag[0][bb]==0)
{
biao[0][bb]=3;
father[0][bb].first=p.first;
father[0][bb].second=p.second;
flag[0][bb]=1;
que.push(P(0,bb));
d[0][bb]=d[aa][bb]+1;
}
if(flag[a][bb]==0)
{
biao[a][bb]=1;
father[a][bb].first=p.first;
father[a][bb].second=p.second;
flag[a][bb]=1;
que.push(P(a,bb));
d[a][bb]=d[aa][bb]+1;
}
if(flag[aa][0]==0)
{
biao[aa][0]=4;
father[aa][0].first=p.first;
father[aa][0].second=p.second;
flag[aa][0]=1;
que.push(P(aa,0));
d[aa][0]=d[aa][bb]+1;
}
if(flag[aa][b]==0)
{
biao[aa][b]=2;
father[aa][b].first=p.first;
father[aa][b].second=p.second;
flag[aa][b]=1;
que.push(P(aa,b));
d[aa][b]=d[aa][bb]+1;
}
}
if(que.empty())printf("impossible\n");
return false;
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c))
{
memset(biao,0,sizeof(biao));
memset(d,0,sizeof(d));
memset(flag,0,sizeof(flag));
if(bfs())
{
P p=first;
while(1)
{
P then=father[p.first][p.second];
// printf("%d %d\n",then.first,then.second);
if(father[p.first][p.second].first==0&&father[p.first][p.second].second==0)
{
break;
}
feng.push(then);
p=then;
}
// printf("%d\n",feng.size());
while(!feng.empty())
{
P p=feng.top();
if(biao[p.first][p.second]==1)printf("FILL(1)\n");
else if(biao[p.first][p.second]==2)printf("FILL(2)\n");
else if(biao[p.first][p.second]==3)printf("DROP(1)\n");
else if(biao[p.first][p.second]==4)printf("DROP(2)\n");
else if(biao[p.first][p.second]==5)printf("POUR(1,2)\n");
else if(biao[p.first][p.second]==6)printf("POUR(2,1)\n");
// printf("%d %d\n",p.first,p.second);
feng.pop();
}
}
}
return 0;
}