题目地址:http://poj.org/problem?id=3414

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12080   Accepted: 5104   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)
#include<stdio.h>
#include<string.h>
#include <queue>
#include <stack>
#include <algorithm> using namespace std; int a, b, c;//a b容器的大小 c目标体积 bool vis[101][101]; struct node
{
char ch;
int x,y;
int pre;
int self;
}op[100000];
int e=0; struct path
{
int u, v;
int cnt;
int pre;
int self;
}; //int road[10000], step; stack<int>p;
bool ok;
int ans; void bfs()
{
queue<path>q;//创建队列
memset(vis, false, sizeof(vis));//初始化未访问 path s; s.u=0; s.v=0; s.cnt=0; s.pre=-1; s.self=-1;
q.push(s); int num=-1;
vis[0][0]=true; ok=false; ans=0; while(!q.empty())
{
path cur=q.front(); q.pop(); if(cur.u==c || cur.v==c){
ans=cur.cnt;
ok=true;
int fa=cur.self;
while(fa!=-1){
p.push(fa);
fa=op[fa].pre;
}
break;
}//如果找到了目标状态 //进行6种操作
path temp;
//Fill A
temp=cur; temp.u=a; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;//标记状态访问
op[e].pre=cur.self;
op[e].ch='F'; op[e++].x=1;
} //File B
temp=cur; temp.v=b; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='F'; op[e++].x=2;
} //Drop A
temp=cur; temp.u=0; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='D'; op[e++].x=1;
}
//Drop B
temp=cur; temp.v=0; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='D'; op[e++].x=2;
} //pour(A, B)
temp.v=cur.u+cur.v; temp.u=0; temp.cnt=cur.cnt+1;
if(temp.v>b){
temp.v=b;//装满B
temp.u=cur.u+cur.v-b;
}
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='P'; op[e].x=1; op[e++].y=2;
} //pour(B, A)
temp.u=cur.u+cur.v; temp.v=0; temp.cnt=cur.cnt+1;
if(temp.u>a){
temp.u=a;//装满A
temp.v=cur.u+cur.v-a;
}
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='P'; op[e].x=2; op[e++].y=1;
} }
} int main()
{
scanf("%d %d %d", &a, &b, &c);
e=0;
bfs(); if(!ok){
printf("impossible\n");
}
else{
printf("%d\n", ans);
while(!p.empty())
{
int dd=p.top(); p.pop(); if(op[dd].ch=='F'){
printf("FILL(%d)\n", op[dd].x );
}
else if(op[dd].ch=='D'){
printf("DROP(%d)\n", op[dd].x );
}
else{
printf("POUR(%d,%d)\n", op[dd].x, op[dd].y );
}
}
} return 0;
}
POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)

代码:

poj 3414 Pots【bfs+回溯路径 正向输出】的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. POJ 3414--Pots(BFS+回溯路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 4179   Special Ju ...

  3. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  4. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  5. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  6. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  7. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  8. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  9. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

随机推荐

  1. PYTHON --WebAPP项目转载(廖雪峰) -- Day 1 -- 搭建开发环境

    Day 1 - 搭建开发环境   搭建开发环境 首先,确认系统安装的Python版本是3.5.x: $ python3 --version Python 3.5.1 然后,用pip安装开发Web Ap ...

  2. php的下载

    前言:你的坚持,终将美好! 今天给大家说一下,php的下载,由于php的下载本身就是一个压缩包,解压缩后即可使用.所以,解压缩的过程不再赘述. 第一步:在浏览器的地址栏输入:http://www.ph ...

  3. 设计模式之备忘录模式(Memento)

    备忘录模式(Memento) 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. Originator(发起人):负责创建一个备忘录 ...

  4. Android自己主动化构建之Ant多渠道打包实践(下)

    前言 上一篇(Android自己主动化构建之Ant多渠道打包实践(上))已经介绍了Android的apk是怎样构建的,本篇博客继续Ant打包的实践过程. 集成友盟统计SDK 这里以友盟统计为例,对各个 ...

  5. mysql的体系架构和存储引擎

    定义数据库和实例 数据库:物理操作系统的文件或其他形式文件类型的集合.在mysql数据库中,数据库文件可以是frm.MYD.MYI.ibd结尾的文件. 实例:MySQL数据库由后台线程以及一个共享内存 ...

  6. L - Points on Cycle(旋转公式)

    L - Points on Cycle Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. 面向对象、接口编程的重要性 python 为什么引入接口interface

    面向对象编程的实践 有个产品,其有10个子产品,现在要统计每日消费数据其中8个子产品的消费入账金额算法相同,2个不同; 拓展性差的糟糕的代码 def ConsumptionSum(product): ...

  8. make tree install 目录树状结构工具安装

    http://futeng.iteye.com/blog/2071867 http://zhou123.blog.51cto.com/4355617/1196415 wget ftp://mama.i ...

  9. vs编译程序不能实现,“未能完成操作 未指定的错误”的解决办法

    1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2005右边的“解决方案”处右键 重新加载,就 ...

  10. 关于付款条件ZTERM 的函数

    函数:PR_WF_PAYMENT_BLOCK_CHECKCALL FUNCTION 'FI_F4_ZTERM' F061 支付的过程条件FI_CHANGE_PAYMENT_CONDITIONS New ...