Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9963   Accepted: 4179   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)


和之前做过的一道二维bfs一样。仅仅只是这个须要回溯路径。非常easy在结构体中加一个变量来记录上一个状态在队列中的下标(手敲的队列比較好。这个时候在用STL队列好像不慷慨便)最后找到满足条件的状态逆向打印路径(由于记录的都是上一个状态)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c,s,e,p;
typedef struct node
{
int v1,v2,cur,pre,op;
};
bool vis[999][999];
int ans[10010];
node que[10010];
void bfs()
{
p=0;s=0;e=0;int pos;
node t={0,0,0,0,0};
que[e++]=t;
vis[0][0]=1;
while(s<e)
{
node f=que[s];pos=s;s++;
if(f.v1==c||f.v2==c)
{
printf("%d\n",f.op);
int tem=pos;
for(int i=0;i<f.op;i++)
{
ans[p++]=que[tem].cur;
tem=que[tem].pre;
}
for(int i=p-1;i>=0;i--)
{
switch(ans[i])
{
case 1:printf("FILL(1)\n");break;
case 2:printf("FILL(2)\n");break;
case 3:printf("DROP(1)\n");break;
case 4:printf("DROP(2)\n");break;
case 5:printf("POUR(2,1)\n");break;
case 6:printf("POUR(1,2)\n");break;
}
}
return ;
}
if(f.v1!=m)
{
t.v1=m;
t.op=f.op+1;
t.v2=f.v2;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=1;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=n)
{
t.v2=n;
t.op=f.op+1;
t.v1=f.v1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=2;
t.pre=pos;
que[e++]=t;
}
}
if(f.v1!=0)
{
t.v1=0;
t.v2=f.v2;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=3;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=0)
{
t.v2=0;
t.v1=f.v1;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=4;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=0&&f.v1!=m)
{
t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=5;
t.pre=pos;
que[e++]=t;
}
}
if(f.v1!=0&&f.v2!=n)
{
t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=6;
t.pre=pos;
que[e++]=t;
}
}
}
puts("impossible");
}
int main()
{
while(cin>>m>>n>>c)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

POJ 3414--Pots(BFS+回溯路径)的更多相关文章

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

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

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

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

  3. POJ 3414 Pots bfs打印方案

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

  4. POJ 3414 Pots(BFS)

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

  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/DFS)

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

  7. poj 3414 Pots bfs+模拟

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

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

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

  9. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. Mit 分布式系统导论,Distributed Systems ,lab1 -lab6 总结,实验一到实验六总结

    终于把Mit的分布式系统导论课的实验1-6写完了 做得有些痛苦,但是收获也很大 http://pdos.csail.mit.edu/6.824-2012/labs/index.html 把实验1-6用 ...

  2. Android于fragment_main.xml文件问题组件收购

    package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...

  3. CentOS 6.5安全加固及性能优化

    (文章来自:http://www.cnblogs.com/seasonzone/p/3526296.html) 我们可以通过调整系统参数来提高系统内存.CPU.内核资源的占用,通过禁用不必要的服务.端 ...

  4. 询问任意区间的min,max,gcd,lcm,sum,xor,or,and

    给我们n个数,然后有m个询问,每个询问为L,R,询问区间[L,R]的最大最小值,最小公约数,最大公约数,和,异或,或,且 这些问题通通可以用RMQ的思想来解决. 以下用xor来作为例子 设dp[i][ ...

  5. 用JavaScript实现网页动态水印

    1.基本原理 页面加载后,通过javascript创建页面元素div,并在div元素中创建文本节点,展示水印内容 设置div元素样式,将其zIndex设置一个较高的值,并设置透明度,实现浮在页面的水印 ...

  6. c#为了实现自己的线程池功能(一)

    线程池的技术背景 在面向对象编程中,创建和销毁对象是非常费时间的,由于创建一个对象要获取内存资源或者其他很多其他资源,所以提高服务程序效率的一个手段就是尽可能降低创建和销毁对象的次数.特别是一些非常耗 ...

  7. java中处理字符编码(网页与数据库)(转)

    首先声明一下,此文章时从网上转载的.如下的某些方法是确实管用,但是从中发现了有一点不足,就是原文笔者没考虑使用不同Web Server时出现的情况,比如文章里我用红色字体画出来的部分代码在Tomcat ...

  8. 【原创】leetCodeOj --- Binary Tree Right Side View 解题报告

    二连水 题目地址: https://leetcode.com/problems/binary-tree-right-side-view/ 题目内容: Given a binary tree, imag ...

  9. 批处理bat脚本编写(附详细例子)

                                                        批处理bat脚本编写(附详细例子) 由于在项目开发的过程中经常需要编写bat脚本,而看大牛们编写 ...

  10. acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

    称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等 ...