Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12198   Accepted: 5147   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 ≤ i ≤ 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 A, B, 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

Northeastern Europe 2002, Western Subregion

空杯子倒水问题,这题比较简单,只有两个杯子,最初的时候都是空的,要倒出指定量的水有三种操作:

1、FILL(i)        把第i个杯子装满(i=0,1)

2、DROP(i)      把第i个杯子倒空

3、POUR(i,j)    把i的水倒入到j中,直到j满或i倒完

我的想法:把a->b,b->c,。。。。共6种倒水方法一个一个列出来,而每种都是一样的讨论方法,

虽然很好做,但对于我这样的入门级水手来说还是写不出太好看的代码,所以。。。。

看看吧,看不懂再去看看网上别人的==||

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
bool vis[maxn][maxn];
int fa[maxn][maxn];
int op[maxn][maxn];
int sa,sb,sc;
struct node{
int a,b,step;
};
char ans[][]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void print(int a ,int b){
if(op[a][b]==-)
return;
else{
print(fa[a][b]/,fa[a][b]%);
printf("%s\n",ans[op[a][b]]);
}
} bool bfs(){
node u,v;
u.a=,u.b=;
u.step=;
queue<node>q;
q.push(u);
memset(op,,sizeof(op));
memset(vis,false,sizeof(vis));
memset(fa,,sizeof(fa));
op[][]=-;
fa[][]=;
vis[][]=true;
while(!q.empty()){
u=q.front();
q.pop();
if(u.a==sc||u.b==sc){
printf("%d\n",u.step);
print(u.a,u.b);
return true;
}
v=u; if(u.a!=sa){
v.a=sa;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b!=sb){v.b=sb;
if(!vis[v.a][v.b]){
v.step=u.step+;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){v.a=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b){v.b=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){
if(v.a>=sb-u.b&&u.b!=sb){v.a-=(sb-u.b);v.b=sb;}
else if(v.a<sb-u.b){v.a=;v.b+=u.a;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b)
{
if(v.b>=sa-u.a&&u.a!=sa){v.b-=(sa-u.a);v.a=sa;}
else if(v.b<sa-u.a){v.b=;v.a+=u.b;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
} }
return false;
}
int main(){
while(scanf("%d%d%d",&sa,&sb,&sc)!=EOF){
bool flag=bfs(); if(!flag)
printf("impossible\n");
}
return ;
}

poj3414 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. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  3. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  4. Pots(BFS)

    Pots Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submiss ...

  5. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  6. POJ 3414 Pots(罐子)

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

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. Django疑难问题

    1页面出现中文报错 :Non-ASCII character '\xe9' in file E:\CPaas\cpaas\views.py 解决:在页面顶部加入#coding=utf-8 2执行syn ...

  2. SRM 670 div2 A B C div1 A(贪心,子问题合并)

    A Cdgame brute force... B Drbalance 贪心,每次选最前面的-变成+,相当于后面所有的负值+2. C Treestrat 考虑集中去抓一个Red Token,以这个To ...

  3. 【BZOJ1087】[SCOI2005] 互不侵犯King(状压DP)

    点此看题面 大致题意: 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案(国王能攻击到它周围的8个格子). 状压\(DP\) 一看到这道题我就想到了经典的八皇后问题,但 ...

  4. 【洛谷1486】[NOI2004] 郁闷的出纳员(Splay的小运用)

    点此看题面 大致题意: 你是一个公司的出纳员,现在有\(n\)个操作,操作有4种:新来一个员工.增加全体员工工资.减少全体员工工资.查询第\(k\)多的工资.若一个员工的工资在某一时刻低于合同上的工资 ...

  5. BSGS算法初探

    前言 \(BSGS\)算法,全称\(Baby\ Step\ Giant\ Step\),即大小步算法.某些奆佬也称其为拔(Ba)山(Shan)盖(Gai)世(Shi)算法. 它的主要作用是求解形式如\ ...

  6. 第四章 用javascript和DOM去建立一个图片库

    把整个图片库的浏览链接集中安排在你的图片库里,只在用户点击了这个主页里的某个图片链接时才把相应的图片传送给它. 代码如下: <body> <ul> <li> < ...

  7. matlab一次读取多张图片

    实验平台:matlab R2010Rb 读取C:\Users\KCl\Documents\MATLAB\SRCNN\Set5文件夹下所有bmp文件,并存储到im字典中 clear all clc im ...

  8. 洛谷P2908 [USACO08OPEN]文字的力量Word Power

    题目描述 Farmer John wants to evaluate the quality of the names of his N (1 <= N <= 1000) cows. Ea ...

  9. 【Django】URL中传递中文的问题

     开发环境:Ubuntu16.04+Django 1.11.9+Python2.7 在开发中,在做查找某些信息这个功能的时候,遇到的一个问题.需要在URL中传递查找的关键字,当关键字为中文的时候,并不 ...

  10. Centos 安装python 3.7 ,同时兼容python2.7

    下载Python源码 从http://www.python.org/download/根据需要的版本下载源文件. 例如上图就是我在官网直接找到3.5.6版本的下载页面,点击的tar源码包进行下载. 1 ...