Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i)        fill the pot i ( ≤ i ≤ ) from the tap;
DROP(i) empty the pot i to the drain;
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 to 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 Sample Output FILL()
POUR(,)
DROP()
POUR(,)
FILL()
POUR(,)

题意 有两个杯子体积a和b升,有六个操作,经过几次操作可使至少其中一杯水有c升,并输出操作方法

方法 利用广搜,每种状态由可操作的状态向下搜索,找到一种就为操作次数最少的,找到输出操作方法

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include <math.h>
#include<queue>
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
#define N 405
using namespace std;
char str[][]={"FILL(2)","FILL(1)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};
int n,m,k,f;
int vis[N][N];
struct node
{
int x,y,t;
char s[];///记录路径过程 s[i] i代表第i步做str[s[i]]这个操作
};
void dfs()
{
queue<node> Q;
node q,p;
q.x=;
q.y=;
q.t=;
strcpy(q.s,"");///记得初始化
vis[][]=;
Q.push(q);
while(Q.size())
{
p=Q.front();
Q.pop();
if(p.x==k || p.y==k)
{
f=;
printf("%d\n",p.t);
for(int i=; i<=p.t; i++)
printf("%s\n",str[p.s[i]-'']);///根据p.s储存的输出路径
break;
}
for(int i=; i<; i++)///每次一共就有六种操作可做,符合条件的才实行
{
q.t=p.t+;
q.x=;
q.y=;
strcpy(q.s,p.s);
q.s[q.t]=i+'';
if(i== && !p.y)///第二个杯子为空,装满
{
q.x=p.x;
q.y=m; }
else if(i== && !p.x)///第一个杯子为空,装满
{
q.x=n;
q.y=p.y;
}
else if(i== && p.x<n&&p.y)///第一个杯子不满,第二个杯子不空,把第二个杯子的水倒入第一个杯子里
{
if(p.x+p.y<=n)
{
q.x=p.x+p.y;
q.y=;
}
else
{
q.x=n;
q.y=p.x+p.y-n;
}
}
else if(i== && p.y<m && p.x)///第二个杯子不满,第一个杯子不空,把第一个杯子的水倒入第二个杯子里
{
if(p.x+p.y<=m)
{
q.x=;
q.y=p.x+p.y;
}
else
{
q.y=m;
q.x=p.x+p.y-m; }
}
else if(i== && p.x)///第一个杯子不空,倒完
{
q.x=;
q.y=p.y;
}
else if(i== && p.y)///第二个杯子不空,倒完
{
q.y=;
q.x=p.x;
}
if(!vis[q.x][q.y])///是否出现过,防止重复出现,死循环
{
vis[q.x][q.y]=;
Q.push(q);
}
}
}
return ;
}
int main()
{
while(scanf("%d %d %d",&n,&m,&k)!=EOF)
{
met(vis,);
f=;
dfs();
if(f==)
printf("impossible\n");
}
return ;
}

(poj)3414 Pots (输出路径的广搜)的更多相关文章

  1. POJ 3414 Pots 记录路径的广搜

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

  2. POJ 3414 Pot (输出路径)【BFS】

    <题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯 ...

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

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

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

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

  5. POJ 3414 Pots(罐子)

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

  6. BFS POJ 3414 Pots

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

  7. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  8. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

  9. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

随机推荐

  1. 新花生壳内网版2.3 + Tomcat7 搭建自己的网站(2015.01.21)

    网上很多资料,问题主要是出在 tomcat 的访问上而已: 如下总结一下: 首先在 花生壳 官网(http://hsk.oray.com/)注册一个帐号,每个帐号可以领取一个免费域名 然后下载安装新版 ...

  2. deep learning(1)BP神经网络原理与练习

    具体原理参考如下讲义: 1.神经网络 2.反向传导 3.梯度检验与高级优化 看完材料1和2就可以梳理清楚bp神经网络的基本工作原理,下面通过一个C语言实现的程序来练习这个算法 //Backpropag ...

  3. c++学生成绩管理系统

    虽然比较水 =.= 但是写了两节课+一个中午 都是强迫症的锅 http://www.cnblogs.com/wenruo/p/4940182.html #include <cstdio> ...

  4. Android多线程研究(4)——从一道面试题说起

    有一道这种面试题:开启一个子线程和主线程同一时候运行,子线程输出10次后接着主线程输出100次,如此重复50次.先看以下代码: package com.maso.test; /** * * @auth ...

  5. Android开发_Gson解析

    //转换器 GsonBuilder builder = new GsonBuilder(); // 不转换没有 @Expose 注解的字段 builder.excludeFieldsWithoutEx ...

  6. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  7. j2ee学习笔记 javascript 学习

    JavaScript 组成: ECMAScript + BOM + DOM Window对象是JS中的顶层对象 ECMAScript: 规定了一些语法,变量,for循环等等结构 BOM: Browse ...

  8. java float、double精度研究(转)

    在java中运行一下代码System.out.println(2.00-1.10);输出的结果是:0.8999999999999999很奇怪,并不是我们想要的值0.9 再运行如下代码:System.o ...

  9. Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现

    有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...

  10. 第一个felx项目的创建

    使用新建flex项目向导建立项目