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. Java_JSP自定义标签的开发与应用

    在JSTL提供了四个标签库(核心标签库.国际化标签库.数据库标签库和XML标签库),涉及到了几十个标签.虽然这些标签可以完成比较复杂的工作,但它们仍然无法满足程序中的特殊需求.因此,就需要用户根据自己 ...

  2. [Dynamic Language] Python3.7 源码安装 ModuleNotFoundError: No module named '_ctypes' 解决记录

    Python3.7 源码安装 ModuleNotFoundError: No module named '_ctypes' 解决记录 源码安装时报错 File "/home/abeenser ...

  3. android四大组件--ContentProvider具体解释

    一.相关ContentProvider概念解析: 1.ContentProvider简单介绍 在Android官方指出的Android的数据存储方式总共同拥有五种,各自是:Shared Prefere ...

  4. SpringMVC怎么获取前台传来的数组

    var tollerlist = new Array(); for(var k in objToller){ tollerlist.push(k); } $.ajax({ type:"pos ...

  5. SonarQube使用

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,下面将会介绍一下这个工具的安装.配置以及使用. 一.安装 1.下载好sonarqube后,解压打开bin目录,启 ...

  6. RocketMQ的部署方式及持久化方式

    RocketMQ 的 Broker 有三种集群部署方式: 1. 单台 Master 部署: 2. 多台 Master部署: 3. 多 Master 多 Slave 部署:采用第 3 种部署方式时, M ...

  7. mac 拷贝文件时报错 8060 解决方案

    解决如下: 即某文件夹下出现多重子目录,级数很多,删除多余的子文件夹即可. 至于如何产生的,有人说是xcode升级导致,不过没有见证 .我的不属于这类情况的. (参见:http://macosx.co ...

  8. 【Linux】在虚拟机上安装CentOS7

    在配置好的机子上,可以装个双系统,但是在我自己的本子上,磁盘读写太垃圾了,连压缩卷 都执行不了,分不出空间,装不了CentOS系统,没办法,采用虚拟机的方式,把它转起来. -------------- ...

  9. 【linux】linux重启tomcat + 实时查看tomcat启动日志

    linux重启tomcat命令: http://www.cnblogs.com/plus301/p/6237468.html linux查看toncat实时的启动日志: https://www.cnb ...

  10. python测试开发django-9.使用navicat连接mysql

    前言 navicat 是一个连接数据库的可视化工具,可以连接mysql和oracle做一些简单增删改查,对于初学者来说非常方便的 navicat安装 navicat版本比较多,分享一个我经常用的版本 ...