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. centos7安装zookeeper3.4.9集群

    本篇文章目的:以最小成本学习zookeeper的集群安装. zookeeper的三要素: 1.一致,能够保证数据的一致性 2.有头,始终有一个leader,node/2+1个节点有效,就能正常工作 3 ...

  2. 运用Android ROM Manager应用安装ClockworkMod Recovery的详细教程

    在安装ClockworkMod Recovery恢复模式之前,建议先认识下Google Android平台的ClockworkMod Recovery恢复模式 对于Android ROM Manage ...

  3. java 盒子模型

    http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/

  4. 03把IL编译成可执行文件

    1.在记事本中编写IL代码如下: .assembly HelloWorld{} .assembly extern mscorlib{}   .method public static void Mai ...

  5. Windows系统虚拟内存文件和休眠缓存大小优化

    虚拟内存的大小设置 虚拟内存的文件 pagefile.sys 一般在系统盘的根目录下,默认情况下会比较大.下面给出缩小设置方式. 我的电脑(鼠标右键)--属性--高级系统设置--切换到“高级”选项卡- ...

  6. java基础知识概要图

  7. spring开发文档收集

    http://docs.spring.io/spring/docs/4.2.1.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#aop-at ...

  8. C语言:返回两个数组中第一个元素的指针,并输出这个值

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年. All righ ...

  9. C#中图片透明【转】

    C#中图片透明 /// <summary> /// 处理图片透明操作 /// </summary> /// <param name="srcImage" ...

  10. C++11中万能的可调用类型声明std::function<...>

    在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...