Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 14052 | Accepted: 5919 | 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)
题意:给你两种容量的罐子,让你经过几种操作,能否凑出指定容量的水
1.FILL(i) 把i罐子灌满
2.DROP(i) 把i罐子清空
3.POUR(i,j) 把i罐子的水倒进j罐子里去,如果不能全部倒进去,就剩到罐子里,能倒了,就清空了。
有6中入口,注意要标记状态(标记执行完的状态),用string存操作(网上看的,自己不会),存到每一个状态以前经过的操作,还有别的存法,还没学会
#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;