http://poj.org/problem?id=3414

                                      Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9996   Accepted: 4198   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 ≤ 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.

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,

对A、B可以有如下操作:

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

问经过哪几个操作后能使得任意一个瓶子的残余水量为C。

若不可能得到则输出impossible

解题思路:
6入口的bfs

把两个两个桶的某个同一时间的状态看做一个整体,视为初态

可对初态进行六种操作,即FILL(1),FILL(2),DROP(1),DROP(2),POUR(1,2),POUR(2,1)

这6种操作在我的程序中分别用一个整数进行记录;

1z0: 清空z瓶子

2z0: 装满z瓶子

3xy: 从x瓶倒向y瓶

(x,y,z∈{1,2})

这样在输出操作时就能根据数字的特点 进行选择性输出 了

最后我简单说说各种操作的数学表达式:

设1瓶的容量为v1,残余水量为k1, 2瓶的容量为v2,残余水量为 k2

那么 fill(1) 相当于 k1=v1  fill(2)相当于k2=v2  drop(1)相当于k1=0   drop(2)相当于k2=0

对于Pour(1,2),若k1+k2<=v2,则 k2=k1+k2 , k1=0  (有先后顺序)

否则k1=k1+k2-v2  , k2=v2  (有先后顺序)

Pour(2,1)亦同理

利用pre[i]数组来存储当前节点的父节点的位置信息。

之前不会回溯,感到这题无从下手,之后要多做这种题。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int a,b,c,s,e;
int v[][];
struct node
{
int x,y,ans,d,d1;
}q[],t,f;
int pre[],p[];
void FILI(int xx,int yy,int zz,int i,int ff)
{
if(i==)
{
f.x=a;
f.y=yy;
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
else if(i==)
{
f.x=xx;
f.y=b;
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
}
void POUR(int xx,int yy,int zz,int i,int ff)
{
if(i==)
{
f.x=;
f.y=yy;
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
else if(i==)
{
f.x=xx;
f.y=;
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
}
void D(int xx,int yy,int zz,int i,int ff)
{
if(i==)
{
if(xx+yy>=a)
{
f.x=a;
f.y=yy+xx-a;
}
else
{
f.x=xx+yy;
f.y=;
}
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
else if(i==)
{
if(xx+yy>=b)
{
f.y=b;
f.x=xx+yy-b;
}
else
{
f.y=xx+yy;
f.x=;
}
if(v[f.x][f.y]==)
{
f.ans=zz+;
f.d=;
f.d1=;
q[e]=f;
pre[e]=ff;
e++;
v[f.x][f.y]=;
}
}
}
void print(int s)
{
int ss=;
for(int i=s;i!=-;i=pre[i])
{
p[ss++]=i;
}
for(int i=ss-;i>=;i--)
{
if(q[p[i]].d==||q[p[i]].d1==)
{
if(q[p[i]].d==)
{
printf("FILL(1)\n");
}
else printf("FILL(2)\n");
}
else if(q[p[i]].d==||q[p[i]].d1==)
{
if(q[p[i]].d==)
{
printf("DROP(1)\n");
}
else printf("DROP(2)\n");
}
else if(q[p[i]].d==||q[p[i]].d1==)
{
if(q[p[i]].d==)
{
printf("POUR(2,1)\n");
}
else printf("POUR(1,2)\n");
}
}
}
void BFS()
{
e=;
s=;
memset(pre,-,sizeof(pre));
memset(v,,sizeof(v));
v[][]=;
t.x=;
t.y=;
t.ans=;
t.d=;
t.d1=;
q[e++]=t;
while(s<e)
{
t=q[s++];
if(t.x==c||t.y==c)
{
printf("%d\n",t.ans);
print(s-);
return ;
}
for(int i=;i<;i++)
{
if(i==)
FILI(t.x,t.y,t.ans,i,s-);
else if(i==)
FILI(t.x,t.y,t.ans,i,s-);
else if(i==)
POUR(t.x,t.y,t.ans,i,s-);
else if(i==)
POUR(t.x,t.y,t.ans,i,s-);
else if(i==)
D(t.x,t.y,t.ans,i,s-);
else if(i==)
D(t.x,t.y,t.ans,i,s-);
}
}
printf ("impossible\n");
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
BFS();
}
return ;
}

最新文章

  1. Doc
  2. 三、jQuery--jQuery基础--jQuery基础课程--第9章 jQuery 常用插件
  3. const 指针的三种使用方式
  4. [OrangePi] If you are using an older image
  5. java动态代理Proxy
  6. ListView真的蛮好用
  7. 【翻译】五步快速使用LINQPad尝鲜StreamInsight
  8. Unity扩展 自定义事件Send组件
  9. c 计算Fibonacci数列:1,1,2,3,5,8,13……这题也是很经典。
  10. Winform Timer用法,Invoke在Timer的事件中更新控件状态
  11. Java NIO核心组件简介
  12. Orange Greenworks
  13. Winhex数据恢复笔记(五)
  14. Python解析HDF文件 分类: Python 2015-06-25 00:16 743人阅读 评论(0) 收藏
  15. Difference Between InnoDb and MyISAM(个人觉着是好文章,简单易懂,推荐看)
  16. hexo + Github Page 0元建立博客攻略
  17. 8、redis之事务1-redis命令
  18. 「要买车网」免费获取汽车电商要买车网购车优惠券 - 持续更新(2016-03-12)www.fortunelab.cn
  19. &lt;泛&gt; 并查集
  20. APP三种开发模式

热门文章

  1. 【WinForm程序】注册热键快捷键切换
  2. 链表一元多项式计算器的实现(Java语言描述)
  3. 通俗大白话来理解TCP协议的三次握手和四次分手
  4. LinQ实战学习笔记(一) LINQ to (Objects, XML, SQL) 入门初步
  5. Ubuntu Eclipse配置Python开发环境
  6. nginx expires配置
  7. 【JSP】JSP指令
  8. [通信] C# TCP实现多个客户端与服务端 数据 与 文件的传输
  9. geotrellis使用(三十二)大量GeoTiff文件实时发布TMS服务
  10. Js中对id和class属性进行模糊查询