链接:



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

Northeastern Europe 2002, Western Subregion


算法:数组模拟bfs


题意:


给你两个容器 A  B 问是否能够经过有限的步骤倒水,得到容量为 C 的水
输出最小的步数,同时输出每一步的操作。
如果不能达到目标状态,则输出 impossible

思路:


总状态只有那么多, 反正每一步后都只有 6 种操作明显的 BFS 关键是每一步路径的记录。

开始用 BFS + 容器做了一遍,发现还是无法处理好输出路径问题,只好重新开始用数组模拟。
容器虽然很好用又方便,但是在不考虑内存的状况下,效率终究比不上数组模拟的了。

注意到 A 和 B 的范围是 1 到 100 的整数,
那么我们可以用vis[i][j]来记录每一种状态 0 <= i, j <= 100 ;
 i 表示目前 A 容器中的水, j 表示目前 B 容器中的水

应该很容易就能分析出,对应于每一种状态的下一步只有六种情况:

一:用水池中的水装满 A
二:用水池中的水装满 B
三:把 A 中的水全部倒进废水池
四:把 B 中的水全部倒进废水池
五:把 A 中的水倒进 B 【不能溢出】
    那么对应可能会有两种状态:用 k1 表示 A 中的水, k2 表示 B 中的水
    如果 k1+k2 <= B 则   k2 = k1+k2; k1 = 0 【不能够装满容器 B】注意顺序
    否则 k1 = k1+k2-B; k2 = B 【能够装满容器 B】
六:把 B 中的水倒进 A 【不能溢出】
    也有两种情况,分析同上
    如果 k1+k2 <= A 则 k1 = k1+k2; k2 = 0;
    否则 k2 = k1+k2-A; k1 = A

用结构体数组来模拟队列
用 k1,k2 来记录当前容器中水的状态
前面已经分析过对应于每种情况只有 6 种操作, 那么对应每种情况的操作记录为 1 到 6 输出时处理下就好了。
当然少不了记录到当前状态最少用了多少步数 step
因为要记录路径,所以定义一个 pre 来记录上一步在数组模拟队列中的下标。

最后如果能够达到目的,在判定最后一步的时候记录下最后一步在数组中的编号 lastIndex
然后从lastIndex从后往前找【pre】前一个步骤在数组中的编号存在 id[] 中
最后再按照扫描出的路径依次遍历即可。

code:


3414 Accepted 228K 0MS C++ 4165B

#include<stdio.h>
#include<string.h> const int maxn = 110;
int vis[maxn][maxn]; //标记状态是否入队过
int a,b,c; //容器大小
int step; //最终的步数
int flag; //纪录是否能够成功 /* 状态纪录 */
struct Status{
int k1,k2; //当前水的状态
int op; //当前操作
int step; //纪录步数
int pre; //纪录前一步的下标
}q[maxn*maxn];
int id[maxn*maxn]; //纪录最终操作在队列中的编号
int lastIndex; //最后一个的编号 void bfs()
{
Status now, next; int head, tail;
head = tail = 0; q[tail].k1 = 0; q[tail].k2 = 0;
q[tail].op = 0; q[tail].step = 0; q[tail].pre = 0; tail++; memset(vis,0,sizeof(vis));
vis[0][0] = 1; //标记初始状态已入队 while(head < tail) //当队列非空
{
now = q[head]; //取出队首
head++; //弹出队首 if(now.k1 == c || now.k2 == c) //应该不会存在这样的情况, c=0
{
flag = 1;
step = now.step;
lastIndex = head-1; //纪录最后一步的编号
} for(int i = 1; i <= 6; i++) //分别遍历 6 种情况
{
if(i == 1) //fill(1)
{
next.k1 = a;
next.k2 = now.k2;
}
else if(i == 2) //fill(2)
{
next.k1 = now.k1;
next.k2 = b;
}
else if(i == 3) //drop(1)
{
next.k1 = 0;
next.k2 = now.k2;
}
else if(i == 4) // drop(2);
{
next.k1 = now.k1;
next.k2 = 0;
}
else if(i == 5) //pour(1,2)
{
if(now.k1+now.k2 <= b) //如果不能够装满 b
{
next.k1 = 0;
next.k2 = now.k1+now.k2;
}
else //如果能够装满 b
{
next.k1 = now.k1+now.k2-b;
next.k2 = b;
}
}
else if(i == 6) // pour(2,1)
{
if(now.k1+now.k2 <= a) //如果不能够装满 a
{
next.k1 = now.k1+now.k2;
next.k2 = 0;
}
else //如果能够装满 b
{
next.k1 = a;
next.k2 = now.k1+now.k2-a;
}
} next.op = i; //纪录操作
if(!vis[next.k1][next.k2]) //如果当前状态没有入队过
{
vis[next.k1][next.k2] = 1; //标记当前状态入队
next.step = now.step+1; //步数 +1
next.pre = head-1; //纪录前一步的编号 //q.push(next);
//q[tail] = next; 加入队尾
q[tail].k1 = next.k1; q[tail].k2 = next.k2;
q[tail].op = next.op; q[tail].step = next.step; q[tail].pre = next.pre;
tail++; //队尾延长 if(next.k1 == c || next.k2 == c) //如果达到目标状态
{
flag = 1; //标记成功
step = next.step; //纪录总步骤数
lastIndex = tail-1; //纪录最后一步在模拟数组中的编号
return;
}
}
}
} } int main()
{
while(scanf("%d%d%d", &a,&b,&c) != EOF)
{
flag = 0; //初始化不能成功
step = 0; bfs();
if(flag)
{
printf("%d\n", step); id[step] = lastIndex; //最后一步在模拟数组中的编号
for(int i = step-1; i >= 1; i--)
{
id[i] = q[id[i+1]].pre; //向前找前一步骤在模拟数组中的编号
} for(int i = 1; i <= step; i++)
{
if(q[id[i]].op == 1)
printf("FILL(1)\n"); else if(q[id[i]].op == 2)
printf("FILL(2)\n"); else if(q[id[i]].op == 3)
printf("DROP(1)\n"); else if(q[id[i]].op == 4)
printf("DROP(2)\n"); else if(q[id[i]].op == 5)
printf("POUR(1,2)\n"); else if(q[id[i]].op == 6)
printf("POUR(2,1)\n");
}
}
else printf("impossible\n");
}
return 0;
}


小结:


其实这道题想来是大一第一学期最后一次学长上课,08级的黄超学长提到过的
两年了,一直到前几天居然都没有思路,昨天觉得还是要把基础的搜索练一下,又重新拿出来做,仔细分析了,题目还是很简单的,有时候还是过于浮躁。
做完后,1A 发现这题居然是在POJ 上直接  AC 的第一百题,两年了,今天才破百,博客量都快赶上刷题量了。实在是值得好好反思,有些时候很多东西真的得靠刷题量才能搞上去,所以这两年来,自己一直这么弱,现在只能做新生的题目,hdu 的多校完全沾不上边,也是有原因的。。。
然后和 kuangbin 大神吐槽了下, 他和我说,和他组队刷多校的有个人,两个月就刷了 2000+ 直跃 POJ 第八 http://poj.org/userstatus?user_id=neko13虽然明显不科学,但是也证明了人家肯搞还是能做出很多题的,疯狂吧,再不疯狂我们都老了。

POJ 3414 Pots【bfs模拟倒水问题】的更多相关文章

  1. poj 3414 Pots bfs+模拟

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

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

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

  3. POJ 3414 Pots bfs打印方案

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

  4. poj 3414 Pots(bfs+输出路径)

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

  5. POJ 3414 Pots(BFS)

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

  6. POJ 3414 Pots (BFS/DFS)

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

  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 Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. Centos7 修改运行级别

    systemd使用比sysvinit的运行级别更为自由的target概念作为替代 第三运行级: multi-user.target 第五运行级: graphical.target   #前者是符号链接 ...

  2. InputStream的封装类

    package ex03.pyrmont.connector.http; import java.io.IOException; import java.io.InputStream; import ...

  3. win7 进程kill

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接 在使用windows操作时,经常会遇到一些顽固进程大占CPU,很是苦恼:今天就遇到这样的问题,刚写的一个一个分布 ...

  4. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

  5. [BZOJ 1011] [HNOI2008] 遥远的行星 【近似解】

    题目链接: BZOJ - 1011 题目分析 这道题的特别之处在于,答案可以有5%的误差. 嗯..So? 我还是不会,于是看题解. 神犇的题解就是利用这误差范围求一个近似解. 怎么求近似解呢?假如 g ...

  6. Technology Trader

    zoj2071:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2071 题意:题意一些零件,每一个零件会有一个花费,然后用这 ...

  7. js图片预览插件,不涉及上传

    小小的几十行代码,很牛逼,很实用. 支持多个图片的预览,只要new多个对象就行了. html如下 <!-- zhouxiang www.zhou-xiang.com --> <!DO ...

  8. WordPress特制字符串URL重定向限制绕过漏洞

    漏洞版本: WordPress 3.6 漏洞描述: Bugtraq ID:62344 CVE ID:CVE-2013-4339 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PH ...

  9. OA系统配置文件

    第一章 web.xml配置文件解读 1. web.xml文件解读 lemon OA系统的核心配置文件都放在spring目录下的具有applicationContext的前缀文件.Classpath后有 ...

  10. 【转】Android:Bluetooth 的打开和关闭--不错

    原文网址:http://www.ifeegoo.com/android-turn-on-and-turn-off-bluetooth.html 摘要:Android 中打开和关闭 Bluetooth ...