Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11661   Accepted: 4940   Special Judge

Description

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 ≤ ≤ 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.

Input

On the first and only line are the numbers AB, 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壶的水,倒掉2壶的水。填满1壶的水,填满2壶的水。把2壶的水倒入1壶,把1壶的水倒入2壶。

然后求想要达到结果C毫升水的最小路径,并输出这个路径。

这个题目形式又是 选择+最小路径 。广度优先搜索无疑。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; queue <int> pot1;
queue <int> pot2; int buzhou[105][105];
string path[105][105];// drop[1] drop[2] fill[1] fill[2] pour[2,1] pour[1,2] int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int A,B,C,flag,v1,v2,i;
cin>>A>>B>>C; memset(buzhou,0,sizeof(buzhou)); pot1.push(0);
pot2.push(0); flag=0;
v1=0;
v2=0;
buzhou[v1][v2]=1; while(pot1.size()&&pot2.size())
{
v1=pot1.front();
v2=pot2.front(); pot1.pop();
pot2.pop(); if(v1==C||v2==C)
{
flag=1;
cout<<buzhou[v1][v2]-1<<endl;
for(i=0;i<path[v1][v2].size();i++)
{
if(path[v1][v2][i]=='1')
cout<<"DROP(2)"<<endl;
else if(path[v1][v2][i]=='2')
cout<<"DROP(1)"<<endl;
else if(path[v1][v2][i]=='3')
cout<<"FILL(1)"<<endl;
else if(path[v1][v2][i]=='4')
cout<<"FILL(2)"<<endl;
else if(path[v1][v2][i]=='5')
cout<<"POUR(2,1)"<<endl;
else if(path[v1][v2][i]=='6')
cout<<"POUR(1,2)"<<endl;
}
break;
}
if(!buzhou[0][v2])//drop1
{
buzhou[0][v2]=buzhou[v1][v2]+1;
path[0][v2] = path[v1][v2]+"2";
pot1.push(0);
pot2.push(v2);
}
if(!buzhou[v1][0])//drop2
{
buzhou[v1][0]=buzhou[v1][v2]+1;
path[v1][0] = path[v1][v2]+"1";
pot1.push(v1);
pot2.push(0);
}
if(!buzhou[v1][B])//fill2
{
buzhou[v1][B]=buzhou[v1][v2]+1;
path[v1][B] = path[v1][v2]+"4";
pot1.push(v1);
pot2.push(B);
}
if(!buzhou[A][v2])//fill1
{
buzhou[A][v2]=buzhou[v1][v2]+1;
path[A][v2] = path[v1][v2]+"3";
pot1.push(A);
pot2.push(v2);
} if(v1+v2<A)//b to a
{
if(!buzhou[v1+v2][0])
{
buzhou[v1+v2][0]=buzhou[v1][v2]+1;
path[v1+v2][0]= path[v1][v2]+"5";
pot1.push(v1+v2);
pot2.push(0);
}
}
else
{
if(!buzhou[A][v1+v2-A])
{
buzhou[A][v1+v2-A]=buzhou[v1][v2]+1;
path[A][v1+v2-A]= path[v1][v2]+"5";
pot1.push(A);
pot2.push(v1+v2-A);
}
} if(v1+v2<B)
{
if(!buzhou[0][v1+v2])
{
buzhou[0][v1+v2]=buzhou[v1][v2]+1;
path[0][v1+v2]= path[v1][v2]+"6";
pot1.push(0);
pot2.push(v1+v2);
}
}
else
{
if(!buzhou[v1+v2-B][B])
{
buzhou[v1+v2-B][B]=buzhou[v1][v2]+1;
path[v1+v2-B][B]= path[v1][v2]+"6";
pot1.push(v1+v2-B);
pot2.push(B);
}
}
}
if(!flag)
cout<<"impossible"<<endl;
system("pause");
return 0;
}

最新文章

  1. 断言(assert)的用法
  2. 【温故而知新-Javascript】使用canvas元素(第一部分)
  3. Python: 解决pip安装源被墙的问题
  4. flume+kafka+storm
  5. jquery sortTable拖拽排序
  6. python的交代一
  7. 过渡到SSAS之一:简单模型认识
  8. 端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区
  9. 简单的interface显式和隐式的实现
  10. 在centos6上实现LAMP的FPM模式
  11. 【Java学习笔记之二十七】Java8中传多个参数时的方法
  12. Oracle创建表空间创建用户和用户授权
  13. BZOJ 2434 阿狸的打字机 | AC自动机
  14. C#实现虚拟控件列表显示100w个控件方法
  15. HTTP Security Header Not Detected未检测到HTTP安全标头
  16. TensorFlow-实战Google深度学习框架 笔记(上)
  17. Java单播、广播、多播(组播)
  18. 使用python语言操作MongoDB
  19. 2018.10.16 NOIP模拟 华莱士(并查集)
  20. 8-2 Party Games uva1610 (贪心)

热门文章

  1. java读写串口数据
  2. 关于requirejs和grunt压缩合并是否矛盾
  3. CodeForces 321C Ciel the Commander
  4. 【Luogu】P2220容易题(快速幂)
  5. [UOJ#221][BZOJ4652][Noi2016]循环之美
  6. bzoj4002 [JLOI2015]有意义的字符串 快速幂
  7. oracle的split函数
  8. 【Vijos1412】多人背包(背包DP)
  9. Head first python前六章小结
  10. 使用systemtap调试Linux内核 :www.lenky.info