Pots

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描写叙述

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 (1 ≤ i ≤ 2) 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.

输入

 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).

输出

 The first line of the output must contain the length of the sequence of operations K.  If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

演示样例输入

3 5 4

演示样例输出

6

提示

FILL(2)

POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

简单搜索题,对于两个空瓶子,容积分别为A B 有6种操作 把A(或B)清空,把A(或B)装满,把A倒入B,把B倒入A 。相应这6种操作,有6种状态。典型的bfs搜索。不多了,仅仅是这题明明说的是单组输入结果答案却要多组输入才对。白白贡献5个WA。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c;
typedef struct node
{
int v1,v2,op;
};
bool vis[999][999];
void bfs()
{
node t={0,0,0};
queue <node> Q;
Q.push(t);
vis[0][0]=1;
while(!Q.empty())
{
node f=Q.front();Q.pop();
if(f.v1==c||f.v2==c)
{
cout<<f.op<<endl;
return ;
}
if(f.v1!=m)
{
t.v1=m;
t.op=f.op+1;
t.v2=f.v2;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=n)
{
t.v2=n;
t.op=f.op+1;
t.v1=f.v1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0)
{
t.v1=0;
t.v2=f.v2;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0)
{
t.v2=0;
t.v1=f.v1;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0&&f.v1!=m)
{
t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0&&f.v2!=n)
{
t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
}
puts("impossible");
}
int main()
{ while(cin>>m>>n>>c)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}

SDUT--Pots(二维BFS)的更多相关文章

  1. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

  2. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  3. BZOJ3577:玩手机(最大流,二维ST表)

    Description 现在有一堆手机放在坐标网格里面(坐标从1开始),坐标(i,j)的格子有s_(i,j)个手机. 玩手机当然需要有信号,不过这里的手机与基站与我们不太一样.基站分为两种:发送站和接 ...

  4. J - Borg Maze +getchar 的使用注意(二维字符数组的输入)

    题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...

  5. csp-s模拟测试50(9.22)「施工(单调栈优化DP)」·「蔬菜(二维莫队???)」·「联盟(树上直径)」

    改了两天,终于将T1,T3毒瘤题改完了... T1 施工(单调栈优化DP) 考场上只想到了n*hmaxn*hmaxn的DP,用线段树优化一下变成n*hmaxn*log但显然不是正解 正解是很**的单调 ...

  6. Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...

  7. Javascript生成二维码(QR)

    网络上已经有非常多的二维码编码和解码工具和代码,很多都是服务器端的,也就是说需要一台服务器才能提供二维码的生成.本着对服务器性能的考虑,这种小事情都让服务器去做,感觉对不住服务器,尤其是对于大流量的网 ...

  8. iOS二维码生成、识别、扫描等

    二维码扫描 前言: 最近的项目中使用到了二维码,二维码这个模块功能也完成:觉得还是有必要总结一下用来做记录.好长时间没有写二维码了都忘记在差不多了,重新拾起来还是挻快的. 二维码使用场景: 生活中有很 ...

  9. 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo

    有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...

随机推荐

  1. Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)

    菜单的概念,现在已经很普及了.Windows系统.Mac.桌面版Linux.Java Swing等,都有可视化菜单.一.Android平台3种菜单  选项菜单(OptionMenu).上下文菜单(Co ...

  2. Map和Collection详解

    Collection     -----List                -----LinkedList    非同步                 ----ArrayList      非同 ...

  3. android音乐播放器开发 SweetMusicPlayer 载入歌曲列表

    上一篇写了播放器的总体实现思路,http://blog.csdn.net/huweigoodboy/article/details/39855653,如今来总结下载入歌曲列表. 代码地址:https: ...

  4. DF标志和串移动指令(movsb/movsw)

    1.标志寄存器的第10位DF,方向标志位.在串处理指令中,控制每次操作后si,di的增减 DF=0,每次操作后,si.di添加 DF=1,每次操作后,si.di减小 我们能够用汇编语法描写叙述movs ...

  5. double long float类型读入读出 double取模 fmod

    The library of fmod is #include <cmath> #include<cstdio> #include<cstdlib> #includ ...

  6. vim插件之delimitMate.vim

    delimitMate.vim--这个插件主要是在插入模式下,用来自动补全括号.引号等 下载地址 http://www.vim.org/scripts/script.php?script_id=275 ...

  7. SQL大小写金额转换

    --功能: 用于将小写的数值翻译成大写的字符串(支持到分,即小数点后两位) --入口参数:@decNum------数字型变量 --返回:字符串 --举例:select dbo.fn_ChnMoney ...

  8. Sqoop 数据导入导出实践

    Sqoop是一个用来将hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如:mysql,oracle,等)中的数据导入到hadoop的HDFS中,也可以将HDFS的数据导入到 ...

  9. SQL 查找存在某内容的存储过程

    --查找存在某表名的存储过程 SELECT distinct b.name from syscomments a,sysobjects b WHERE a.id=b.id and a.TEXT LIK ...

  10. [Chromium文档转载,第004章]Mojo Synchronous Calls

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Synchronous Calls Think carefully before ...