Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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,有六种状态
写起来还是有点烦的,要细心
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include<queue>;
using namespace std;
struct node
{
int av,aw,bv,bw;
int o[],pos;
}s;
bool vis[][];
int f;
node cur,next;
void fill(int i)
{
if(i==)
next.aw=next.av;
else if(i==)
next.bw=next.bv;
}
void drop(int i)
{
if(i==)
next.aw=;
else
next.bw=;
}
void pour(int a,int b)
{
if(a==&&b==){
if(next.aw>next.bv-next.bw)
{
next.aw-=(next.bv-next.bw);
next.bw=next.bv;
}
else
{
next.bw+=next.aw;
next.aw=;
}}
else
{
if(next.bw>next.av-next.aw)
{
next.bw-=(next.av-next.aw);
next.aw=next.av;
}
else
{
next.aw+=next.bw;
next.bw=;
}
}
}
void print(int i)
{
if(i==)
printf("POUR(1,2)\n");
else if(i==)
printf("POUR(2,1)\n");
else if(i==)
printf("FILL(1)\n");
else if(i==)
printf("FILL(2)\n");
else if(i==)
printf("DROP(1)\n");
else if(i==)
printf("DROP(2)\n");
}
bool bfs()
{
memset(vis,false,sizeof(vis));
s.aw=;s.bw=;
s.pos=;
vis[][]=true;
queue<node>Q;
Q.push(s);
while(!Q.empty())
{
cur=Q.front();
Q.pop();
if(f==cur.aw||f==cur.bw)
{
printf("%d\n",cur.pos);
for(int i=;i<cur.pos;i++)
{
print(cur.o[i]);
}
return true;
break;
}
//
if(cur.aw!=&&cur.bw!=cur.bv){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=&&cur.aw!=cur.av){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=cur.av){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=cur.bv){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
{
if(!bfs())printf("impossible\n");
}
return ;
}

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+输出路径)

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

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

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

  4. POJ 3414 Pots (BFS/DFS)

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

  5. poj 3414 Pots bfs+模拟

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

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

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

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

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

  8. BFS POJ 3414 Pots

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

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

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

随机推荐

  1. spring.NET的依赖注入

    谈谈自己了解的spring.NET的依赖注入   spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...

  2. C# 号码归属地查询算法

    C# 号码归属地查询算法(根据Android来电归属地二进制文件查询修改) 前言 近期有个项目需要用到号码归属查询,归属地数据库可能比不上ip138,淘宝上也有卖的-,-! 文本提供一个279188条 ...

  3. ios学习笔记之UIViewControl生命周期

    提到UIViewcontrol,每个人都不会陌生吧!平时实际开发中,每天的实际开发应该都少不了它.学过android的各位亲,也对生命周期这四个字并不陌生,无论是activity,还是service, ...

  4. uva 408 Uniform Generator

    Uniform Generator  Computer simulations often require random numbers. One way to generate pseudo-ran ...

  5. SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)

    SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析) 2013-8-20 1.    SQL查询表的行列转换/小计/统计(with  rollup,with ...

  6. 初始化IoC容器(Spring源码阅读)

    初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...

  7. 迷你MVVM框架 avalonjs 0.82发布

    迷你MVVM框架 avalonjs 0.82发布 本版本最大的改进是启用全新的parser. parser是用于干什么的?在视图中,我们通过绑定属性实现双向绑定,比如ms-text="fir ...

  8. spring不依赖注入得到实体bean

    如题,我们一般用spring的ioc,通过配置注入接口得到这个实现类,现在通过研究公司平台框架发现还有一种方法得到spring文件配置的bean方法,举个例子(注:这个ApplicationConte ...

  9. Kafka consumer处理大消息数据问题

    案例分析 处理kafka consumer的程序的时候,发现如下错误: ERROR [2016-07-22 07:16:02,466] com.flow.kafka.consumer.main.Kaf ...

  10. web开发技术中Servlet技术的概述

    1.servlet是什么:servlet是一个位于服务器端的java应用程序它可以像jsp一样,直接输出信息 servlet类必须继承HttpServlet类,否则,不能称为serlvet servl ...