Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 13186 | Accepted: 5529 | 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)
题目大意:有两个杯子,容积分别为A,B目标是让其中一个杯子里边的水达到C。问最少操作,
一共有3种操作:
drop、倒空杯子里边所有水
fill、让杯子充满水
pour,从一个杯子倒向另一个杯子,水不能溢出
题目分析:
一、一共有六种方案操作:fill 1、fill 2、drop 1、drop 2、pour 2 1、pour 1 2、我们每一种方案都需要枚举出来、其中要强调的两个操作是最后两个操作,切记水不能溢出。
二、标记操作要怎样操作呢?我们可以给各个操作都标记上名字,我让他们分别是0 1 2 3 4 5就能够达到目的了,然后在结构体中保存操作过程,用一个数组记录,当输出的时候,直接对应标号输出字符串即可。
代码较长,我们分块来看;
首先是简单的输出:
now=s.front();
if(now.x==vc||now.y==vc)//达到目标
{
printf("%d\n",now.output);//首先输出操作次数
for(int i=0;i<now.output;i++)
{
switch(now.step[i])//对应输出字符串
{
case 0:printf("FILL(1)\n");break;
case 1:printf("FILL(2)\n");break;
case 2:printf("DROP(1)\n");break;
case 3:printf("DROP(2)\n");break;
case 4:printf("POUR(2,1)\n");break;
case 5:printf("POUR(1,2)\n");break;
}
}
return 1;
}
六种操作:
for(int i=0;i<6;i++)
{
nex=now;
if(i==0)//fill 1
{
nex.x=va;//充满A杯子
nex.y=now.y;//B不动
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==1)//fill 2
{
nex.x=now.x;
nex.y=vb;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==2)//drop 1
{
nex.x=0;//倒空A杯子
nex.y=now.y;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==3)//drop 2
{
nex.x=now.x;
nex.y=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==4)//pour 2 to 1
{
if(now.x+now.y>=va)//如果能倒满A杯子
{
nex.x=va;
nex.y=now.y-(va-now.x);//这里切记不要写错了
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
else//如果并不能倒满A杯子
{
nex.x=now.x+now.y;
nex.y=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
}
if(i==5)//1to2
{
if(now.x+now.y>=vb)//这里我托马应该写vb,写成了va,wa了两个小时。。。。
{
nex.x=now.x-(vb-now.y);
nex.y=vb;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
else
{
nex.y=now.x+now.y;
nex.x=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
}
}
最后上完整的AC代码:
//0 fill(1),1 fill(2),2 drop(1),3drop(2),4pour(2,1),5pour(1,2);
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct bottle
{
int x,y,output;
int step[20000];
}now,nex;
int va,vb,vc;
int vis[1005][1005];
int bfs(int x,int y)
{
queue<bottle>s;
vis[x][y]=1;
now.x=x;
now.y=y;
now.output=0;
for(int i=0;i<20000;i++)
{
now.step[i]=100;
}
s.push(now);
while(!s.empty())
{
now=s.front();
if(now.x==vc||now.y==vc)
{
printf("%d\n",now.output);
for(int i=0;i<now.output;i++)
{
switch(now.step[i])
{
case 0:printf("FILL(1)\n");break;
case 1:printf("FILL(2)\n");break;
case 2:printf("DROP(1)\n");break;
case 3:printf("DROP(2)\n");break;
case 4:printf("POUR(2,1)\n");break;
case 5:printf("POUR(1,2)\n");break;
}
}
return 1;
}
s.pop();
for(int i=0;i<6;i++)
{
nex=now;
if(i==0)
{
nex.x=va;
nex.y=now.y;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==1)
{
nex.x=now.x;
nex.y=vb;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==2)
{
nex.x=0;
nex.y=now.y;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==3)
{
nex.x=now.x;
nex.y=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
if(i==4)//2to1
{
if(now.x+now.y>=va)
{
nex.x=va;
nex.y=now.y-(va-now.x);
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
else
{
nex.x=now.x+now.y;
nex.y=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
}
if(i==5)//1to2
{
if(now.x+now.y>=vb)
{
nex.x=now.x-(vb-now.y);
nex.y=vb;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
else
{
nex.y=now.x+now.y;
nex.x=0;
nex.output=now.output+1;
if(vis[nex.x][nex.y]==0)
{
nex.step[now.output]=i;
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
}
}
}
printf("impossible\n");
return 1;
}
int main()
{
while(scanf("%d%d%d",&va,&vb,&vc)!=EOF)
{
memset(vis,0,sizeof(vis));
bfs(0,0);
}
}