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

Source





    题意:输入3个整数n,m,k,前两个整数代表两个杯子的容量。要求用两个杯子通过倒满。倒空。倒入还有一个杯子等方法使得两个杯子中的当中一个杯子的水量等于k,并输出步骤。
    思路:题目非常easy,就是麻烦。特别是步骤那一部分,须要用到BFS的回溯,通过BFS进行搜索,记录两个杯子的水量,使得后面不得反复。

假设搜不到就输出impossible







#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std; int n,m,k;
int ans;
int v[110][110]; struct node
{
int x;
int y;
int z;
int cnt;
} a[1000010]; void DFS(int kk)
{
int pt = a[kk].cnt;
if(pt<=0)
{
return ;
}
DFS(pt);
if(a[pt].x == 1)
{
if(a[pt].y == 1)
{
printf("FILL(1)\n");
}
else
{
printf("FILL(2)\n");
}
}
else if(a[pt].x == 2)
{
if(a[pt].y == 1)
{
printf("DROP(1)\n");
}
else
{
printf("DROP(2)\n");
}
}
else if(a[pt].x == 3)
{
if(a[pt].y == 1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
}
} void BFS()
{
ans = 1;
queue<node>q;
memset(v,0,sizeof(v));
struct node t,f;
t.x = 0;
t.y = 0;
t.z = 0;
t.cnt = 0;
a[0].x = 0;
a[0].y = 0;
a[0].cnt = 0;
q.push(t);
v[t.x][t.y] = 1;
while(!q.empty())
{
t = q.front();
q.pop();
for(int i=1; i<=3; i++)
{
for(int j=1; j<=2; j++)
{
f.x = t.x;
f.y = t.y;
if(i == 1)
{
if(j == 1 && f.x!=n)
{
f.x = n;
}
else if(j == 2 && f.y!=m)
{
f.y = m;
}
}
else if(i == 2)
{
if(j == 1 && f.x!=0)
{
f.x = 0;
}
else if(j == 2 && f.y!=0)
{
f.y = 0;
}
}
else if(i == 3)
{
if(j == 1 && (f.x!=0 && f.y!=m))
{
if(f.x>=m-f.y)
{
f.x = f.x - m + f.y;
f.y = m;
}
else
{
f.y = f.y + f.x;
f.x = 0;
}
}
else if(j == 2 && (f.y!=0 && f.x!=n))
{
if(f.y>=n-f.x)
{
f.y = f.y - n + f.x;
f.x = n;
}
else
{
f.x = f.x + f.y;
f.y = 0;
}
}
}
if(v[f.x][f.y] == 0)
{
f.cnt = ans;
f.z = t.z + 1;
a[ans].x = i;
a[ans].y = j;
a[ans].cnt = t.cnt;
q.push(f);
v[f.x][f.y] = 1;
if(f.x == k || f.y == k)
{
printf("%d\n",f.z);
DFS(ans);
if(i == 1)
{
if(j == 1)
{
printf("FILL(1)\n");
}
else
{
printf("FILL(2)\n");
}
}
else if(i == 2)
{
if(j == 1)
{
printf("DROP(1)\n");
}
else
{
printf("DROP(2)\n");
}
}
else if(i == 3)
{
if(j == 1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
}
return ;
}
ans++;
}
}
}
}
printf("impossible\n");
} int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
BFS();
}
return 0;
}

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

  1. POJ 3414 Pots bfs打印方案

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

  2. POJ 3414 Pots(BFS)

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

  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(著名倒水问题升级版)

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

  5. POJ 3414 Pots (BFS/DFS)

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

  6. poj 3414 Pots bfs+模拟

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

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

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

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

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

  9. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  10. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

随机推荐

  1. DOM-XML(解析与创建)

    /** * 读取(解析)xml文件 * @author Administrator * */ public class DOMRead { public static void main(String ...

  2. spring---transaction(5)---事务的体系

    1.写在前面 事务的模型为3中: 本地事务模式. 编程事务模式. 声明事务模式. 例子1:本地事务模式 Connection conn=jdbcDao.getConnection(); Prepare ...

  3. CentOS 7提示:ERROR unsupported format character '(0xffffffe7) at/域安装失败,您可以运行下列命令重启您的域:

    别理会,直接装即可,这个错误不影响使用.

  4. (provider: 共享内存提供程序, error: 0 - 管道的另一端上无任何进程。) (Microsoft SQL Server,错误: 233)

    ------------------------------ 无法连接到 IFCA-LIUWEI/SQL2005. ------------------------------其他信息: 已成功与服务 ...

  5. Tracing mysqld Using DTrace

    http://dev.mysql.com/doc/refman/5.6/en/dba-dtrace-server.html MySQL 5.6 Reference Manual -> 5 MyS ...

  6. Eclipse批量替换

    情景: 我需要将项目中所有有"上样板"的字样替换为"PCR板",如果寻找单个页面肯定是很麻烦,而且替换很有可能不全,那么该怎么才能完全替换呢? 解决方法: ec ...

  7. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  8. Json解析教程(四.FastJson 的使用)

    简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...

  9. 数学图形之将曲线(curve)转化成曲面管

    在我关于数学图形的博客中,一开始讲曲线的生成算法.然后在最近的章节中介绍了圆环,还介绍了螺旋管以及海螺的生成算法.一类是曲线,一类是环面,为什么不将曲线变成环的图形,毕竟曲线看上去太单薄了,这一节我将 ...

  10. Navicat Premium 12全系列

    Navicat 是香港卓软数码科技有限公司生产的一系列 MySQL.MariaDB.Oracle.SQLite.PostgreSQL 及 Microsoft SQL Server 的图形化数据库管理及 ...