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.

  就是对两个杯子进行操作了,装满,倒空,倒过去。两个水杯里的水表示一个状态,然后BFS就好,要记录路径的话,记住每一个的父亲,然后最后反着来一遍就好了。

代码如下:

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; int A,B,C;
int rem[][];
int shorem[]; struct state
{
int a,b;
int type;
int faa,fab;
int num; state() {}
}; state sta[][]; void showans(state *e)
{
int cou=; while(e->a||e->b)
{
shorem[cou++]=e->type; e=&sta[e->faa][e->fab];
} cout<<cou<<endl;
for(int i=cou-;i>=;--i)
switch(shorem[i])
{
case :
cout<<"FILL(1)\n";
break;
case :
cout<<"FILL(2)\n";
break;
case :
cout<<"DROP(1)\n";
break;
case :
cout<<"DROP(2)\n";
break;
case :
cout<<"POUR(1,2)\n";
break;
case :
cout<<"POUR(2,1)\n";
break;
} } void slove()
{
queue <state *> que;
state *temp;
int t1,t2,t3; sta[][].faa=sta[][].fab=-;
sta[][].type=;
sta[][].num=;
que.push(&sta[][]); while(!que.empty())
{
temp=que.front();
que.pop(); if(temp->a==C||temp->b==C)
{
showans(temp);
return;
} t1=temp->a;
t2=temp->b; if(sta[A][t2].num==-)
{
sta[A][t2].num=temp->num+;
sta[A][t2].faa=t1;
sta[A][t2].fab=t2;
sta[A][t2].type=; que.push(&sta[A][t2]);
}
if(sta[t1][B].num==-)
{
sta[t1][B].num=temp->num+;
sta[t1][B].faa=t1;
sta[t1][B].fab=t2;
sta[t1][B].type=; que.push(&sta[t1][B]);
}
if(sta[][t2].num==-)
{
sta[][t2].num=temp->num+;
sta[][t2].faa=t1;
sta[][t2].fab=t2;
sta[][t2].type=; que.push(&sta[][t2]);
}
if(sta[t1][].num==-)
{
sta[t1][].num=temp->num+;
sta[t1][].faa=t1;
sta[t1][].fab=t2;
sta[t1][].type=; que.push(&sta[t1][]);
}
t3=min(t1,B-t2);
if(sta[t1-t3][t2+t3].num==-)
{
sta[t1-t3][t2+t3].num=temp->num+;
sta[t1-t3][t2+t3].faa=t1;
sta[t1-t3][t2+t3].fab=t2;
sta[t1-t3][t2+t3].type=; que.push(&sta[t1-t3][t2+t3]);
}
t3=min(t2,A-t1);
if(sta[t1+t3][t2-t3].num==-)
{
sta[t1+t3][t2-t3].num=temp->num+;
sta[t1+t3][t2-t3].faa=t1;
sta[t1+t3][t2-t3].fab=t2;
sta[t1+t3][t2-t3].type=; que.push(&sta[t1+t3][t2-t3]);
}
} cout<<"impossible\n";
} int main()
{
ios::sync_with_stdio(false); for(int i=;i<=;++i)
for(int j=;j<=;++j)
{
sta[i][j].a=i;
sta[i][j].b=j;
} while(cin>>A>>B>>C)
{
for(int i=;i<=;++i)
for(int j=;j<=;++j)
sta[i][j].num=-; slove();
} return ;
}

(简单) POJ 3414 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. POJ 3414 Pots ( BFS , 打印路径 )

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

  3. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  4. POJ 3414 Pots bfs打印方案

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

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

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

  6. POJ 3414 Pots(BFS)

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

  7. POJ 3414 Pots (BFS/DFS)

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

  8. poj 3414 Pots bfs+模拟

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

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

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

随机推荐

  1. PHP开发之路之一--WAMP的安装和配置

    来到新公司,领导说后面一个web系统不用ASP.NET做了,用国外的一个Drupal进行二次开发.这个Drupal是基于PHP的一款开源CMS系统,那就必须要自学PHP咯~ 接下来说说正题吧: 一.安 ...

  2. 转:SSH原理与运用(二):远程操作与端口转发

    作者:阮一峰 (Image credit: Tony Narlock) 七.远程操作 SSH不仅可以用于远程主机登录,还可以直接在远程主机上执行操作. 上一节的操作,就是一个例子: $ ssh use ...

  3. 由浅到深理解java反射

    1.基础概念 class类: 1.1java是面向对象的,但是在java中存在两种东西不是面向对象的 一种是普通的数据类型,这也是封装数据类存在的原因. 二种是静态静态成员. 1.2所以我们首先要理解 ...

  4. C: define many functions using predefine..

    /* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ # ...

  5. JS定时器的使用--数码时钟

    <title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...

  6. java类对象

    不错的文章 原文地址:(转载)java中的Class类与Class对象作者:albert1017 本文用作笔记之用,引用的网上资料: http://www.blogjava.net/formatmys ...

  7. html base2

    <html> <body> <h1>My First Web Page</h1> <p id="demo">A Para ...

  8. 4D(DRG、DLG、DOM、DEM)数据 概念

    抱歉原文链接未知 Technorati 标签: DRG,DLG,DOM,DEM 以下有不同的说法,但是意思都很相近. 一. DOM (数字正射影像图):利用数字高程模型对扫描处理的数字化的航空相片.遥 ...

  9. windbg 之 如何定位进程入口点地址

    载入HelloWorld.exe之后我们看看加载了哪些模块:

  10. mytest 截图