http://poj.org/problem?id=3414

题意 : 大意是说给你两个杯子的体积和一个目标体积,a,b,c,通过对a,b进行6种操作,调出c体积的水,6种操作分别是把a倒满,把b倒满,把a弄成空的,把b弄成空的,把a里边的倒到b里,把b里边的水倒到a里;

思路 : 算不上说多难,反正就是六种操作

#include<cstdio>
#include<cstring>
#include<iostream>
const int maxn = ;
using namespace std;
int a,b,c;
int r,l;
int vis[][];
struct node
{
int cup1,cup2,step;//两个杯子,走的步数
int mark;//进行标记,方便输出
int prev;//记录前一个状态
}ch[maxn],sh,zh;
int stepp,s;
void bfs()
{
/*初始化*/
ch[].cup1 = ;
ch[].cup2 = ;
ch[].step = ;
ch[].mark = ;
l = ,r = ;
vis[][] = ;
/*队列模拟*/
while(l<r)
{
sh = ch[l] ;
if(sh.cup1 == c||sh.cup2 == c)//结束标志
{
stepp = sh.step ;
s = l ;//输出时寻找前驱的
return ;
}
if(sh.cup1 != a)//将杯1倒满
{
zh.cup1 = a ;
zh.cup2 = sh.cup2 ;
zh.step = sh.step+ ;
zh.prev = l ;
zh.mark = ;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;//这个状态存到ch数组里
}
}
if(sh.cup2 != b)//将杯2装满
{
zh.cup2 = b ;
zh.cup1 = sh.cup1 ;
zh.step = sh.step+ ;
zh.mark = ;
zh.prev = l;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;
}
}
if(sh.cup1 != )//将杯1倒空
{
zh.cup1 = ;
zh.cup2 = sh.cup2 ;
zh.mark = ;
zh.prev = l;
zh.step = sh.step+ ;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;
}
}
if(sh.cup2 != )//将杯2倒空
{
zh.cup2 = ;
zh.cup1 = sh.cup1;
zh.mark = ;
zh.prev = l;
zh.step = sh.step+ ;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;
}
}
if(sh.cup1 != )//将杯1水倒到杯2里
{
if(sh.cup1 >= b-sh.cup2)
{
zh.cup1 = sh.cup1-(b-sh.cup2) ;
zh.cup2 = b ;
}
else
{
zh.cup2 = sh.cup1+sh.cup2 ;
zh.cup1 = ;
}
zh.step = sh.step+ ;
zh.mark = ;
zh.prev = l ;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;
}
}
if(sh.cup2 != )//将杯2水倒到杯1里
{
if(sh.cup2 >= a-sh.cup1)
{
zh.cup2 = sh.cup2- (a-sh.cup1);
zh.cup1 = a ;
}
else
{
zh.cup1 = sh.cup1+sh.cup2 ;
zh.cup2 = ;
}
zh.step = sh.step+ ;
zh.mark = ;//进行标记
zh.prev = l ;
if(!vis[zh.cup1][zh.cup2])
{
vis[zh.cup1][zh.cup2] = ;
ch[r++] = zh ;
}
}
l++ ;
}
}
void printff(int i)
{
if(i == )
return ;
printff(ch[i].prev) ;
if(ch[i].mark == ) printf("FILL(1)\n");
else if(ch[i].mark == ) printf("FILL(2)\n");
else if(ch[i].mark == ) printf("DROP(1)\n");
else if(ch[i].mark == ) printf("DROP(2)\n");
else if(ch[i].mark == ) printf("POUR(1,2)\n");
else if(ch[i].mark==) printf("POUR(2,1)\n");
}
int main()
{
while(scanf("%d %d %d",&a,&b,&c)!=EOF)
{
stepp=;
memset(vis,,sizeof(vis));
bfs();
if(stepp != )
{
printf("%d\n",stepp);
printff(s);
}
else printf("impossible\n");
}
return ;
}

POJ3414Pots的更多相关文章

  1. poj3414--Pots(bfs,记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10149   Accepted: 4275   Special J ...

  2. poj3414Pots(倒水BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13231   Accepted: 5553   Special J ...

随机推荐

  1. 网站如何提高PR值

    做了站长这些年,积累些经验,记录下来,分享兄弟. 一.商城类型网站 对于商城型的网站站内优化来说,主要是用户咨询和浏览产品时产生的疑问.比如当某用户看到一些自己感兴趣的产品需要咨询的时候,自然就会寻找 ...

  2. ASP.Net网站部署失败

    部署站点时候,出现如下错误 “/”应用程序中的服务器错误. ---------------------------------------------------------------------- ...

  3. 使用Eclipse开发,Java Compiler中Annotation Processin不出现的解决方案

    第一步:在Eclipse菜单栏中点击Help,在点击inatall New Software 第二步:在Work with中找到 Juno - http://download.eclipse.org/ ...

  4. Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view

    解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...

  5. Optimize date2str function

    /*************************************************** Created Date: 19 Jul 2013 Created By: Jimmy Xie ...

  6. Oracle内存管理理论篇二

    目标 了解oracle内存管理方式 掌握ASMM管理方式 掌握AMM管理方式 监控内存使用 学习一个知识点时,最好先了解其历史.ORACLE近期的版本都对内存管理做了简化,从9i通过PGA_AGGRE ...

  7. PySide 简易教程<三>-------动手写起来

    到目前为止,已经接触的Pyside的界面元素有如下几个:QWidget.QPushButton.QLabel.本次再介绍两个tooltip和messagebox.tooltip是一个鼠标悬浮提示信息, ...

  8. 括弧匹配检验(check)

    /*题目:括弧匹配检验 检验给定表达式中括弧是否正确匹配 (两种括弧“( ) ”“[]" ,正确输出OK,错误则输出wrong. 2016年8月8日07:24:58 作者:冰樱梦 */ # ...

  9. ActiveMQ之MessageListener

    消息的消费者接收消息可以采用两种方式: 1.consumer.receive()或 consumer.receive(int timeout); 2.注册一个MessageListener. 采用第一 ...

  10. Oracle查询出最最近一次的一条记录

    需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...