Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24158 Accepted: 10232 Special Judge
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 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)
Source
Northeastern Europe 2002, Western Subregion
题目简述:
有两个没有刻度只知道容量a,b的瓶子,想办法装出c毫升的水。你可以装满、倒空、互相倾倒。
题目分析:
bfs即可,题目要求输出成功装出c毫升水的过程,我的解法是在struct里加个数组按顺序记录当前状态已经经历的操作。
代码实现:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
#define ms(i,j) memset(i,j,sizeof(i))
int a,b,c,mark[105][105];
struct cup
{
int a,b,step[100000];
} temp,now;
void bfs()
{
queue<cup> line;
temp.a=0;
temp.b=0;
line.push(temp);
while(!line.empty())
{
now=line.front();
line.pop();
if(now.a==c||now.b==c)
{
printf("%d\n",now.step[0]);
for(int i=1; i<=now.step[0]; i++)
{
if(now.step[i]==1)
printf("FILL(%d)\n",1);
if(now.step[i]==2)
printf("FILL(%d)\n",2);
if(now.step[i]==3)
printf("DROP(%d)\n",1);
if(now.step[i]==4)
printf("DROP(%d)\n",2);
if(now.step[i]==5)
printf("POUR(%d,%d)\n",1,2);
if(now.step[i]==6)
printf("POUR(%d,%d)\n",2,1);
}
return;
}
if(mark[now.a][now.b])
continue;
mark[now.a][now.b]=1;
if(now.a!=a)
{
temp=now;
temp.a=a;
temp.step[0]++;
temp.step[temp.step[0]]=1;
line.push(temp);
}
if(now.b!=b)
{
temp=now;
temp.b=b;
temp.step[0]++;
temp.step[temp.step[0]]=2;
line.push(temp);
}
if(now.a)
{
temp=now;
temp.a=0;
temp.step[0]++;
temp.step[temp.step[0]]=3;
line.push(temp);
}
if(now.b)
{
temp=now;
temp.b=0;
temp.step[0]++;
temp.step[temp.step[0]]=4;
line.push(temp);
}
if(now.a!=a)
{
temp=now;
if(temp.a+temp.b<=a)
{
temp.a=temp.a+temp.b;
temp.b=0;
}
else
{
int s=temp.a+temp.b;
temp.a=a;
temp.b=s-a;
}
temp.step[0]++;
temp.step[temp.step[0]]=6;
line.push(temp);
}
if(now.b!=b)
{
temp=now;
if(temp.a+temp.b<=b)
{
temp.b=temp.a+temp.b;
temp.a=0;
}
else
{
int s=temp.a+temp.b;
temp.b=b;
temp.a=s-b;
}
temp.step[0]++;
temp.step[temp.step[0]]=5;
line.push(temp);
}
}
printf("impossible\n");
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
bfs();
}