题目描述 Description

有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水。设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒。已知 x 升壶为空 壶, y 升壶为空壶。问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来。

输入描述 Input Description

一行,三个数据,分别表示 x,y 和 z;

输出描述 Output Description

一行,输出最小步数 ,如果无法达到目标,则输出"impossible"

样例输入 Sample Input

3 22 1

样例输出 Sample Output

14

数据范围及提示 Data Size & Hint

广度优先搜索 深度优先搜索 迭代搜索 搜索

【DFS:】

#include<bits/stdc++.h>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const int N = 1e4+10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[][3]={ {0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0} };
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int f[200][200],a,b,z;
void dfs(int x,int y,int step)
{
if(f[x][y]!=0 && step+1>=f[x][y]) return ;
f[x][y]=step+1;
dfs(x,0,step+1);
dfs(0,y,step+1);
dfs(x,b,step+1);
dfs(a,y,step+1);
if(x+y<=a) dfs(x+y,0,step+1);
else dfs(a,x+y-a,step+1);
if(x+y<=b) dfs(0,x+y,step+1);
else dfs(x+y-b,b,step+1);
} int main()
{
while(~scanf("%d%d%d",&a,&b,&z))
{
memset(f,0,sizeof(f));
int ans=INF;
dfs(0,0,0);
for(int i=0;i<=a;i++)
{
if(f[i][z]!=0)
{
if(f[i][z]<ans)
ans=f[i][z];
}
}
for(int i=0;i<=b;i++)
{
if(f[z][i]!=0)
{
if(f[z][i]<ans)
ans=f[z][i];
}
}
if(ans==INF) printf("impossible\n");
else printf("%d\n",ans-1);
}
}

【BFS】:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int x,y,target;
struct state{
int x,y,step;
}f; queue<state> q;
bool vis[233][233];
int bfs()
{
q.push(f);
vis[f.x][f.y]=1;
while(q.size())
{
f=q.front(); q.pop();
if(f.x==target||f.y==target) return f.step;
if(f.x<x&&vis[x][f.y]==0) //x倒满
{
q.push((state){x,f.y,f.step+1});
vis[x][f.y]=1;
}
if(f.x&&vis[0][f.y]==0) //x倒空
{
q.push((state){0,f.y,f.step+1});
vis[0][f.y]=1;
}
if(f.y<y&&vis[f.x][y]==0) //y倒满
{
q.push((state){f.x,y,f.step+1});
vis[f.x][y]=1;
}
if(f.y&&vis[f.x][0]==0) //y倒空
{
q.push((state){f.x,0,f.step+1});
vis[f.x][0]=1;
}
if(f.x>=y-f.y&&vis[f.x-(y-f.y)][y]==0)//x->y
{
q.push((state){f.x-(y-f.y),y,f.step+1});
vis[f.x-(y-f.y)][y]=1;
}
if(f.x<y-f.y&&vis[0][f.x+f.y]==0)//x->y
{
q.push((state){0,f.x+f.y,f.step+1});
vis[0][f.x+f.y]=1;
}
if(f.y>=x-f.x&&vis[x][f.y-(x-f.x)]==0)//y->x
{
q.push((state){x,f.y-(x-f.x),f.step+1});
vis[x][f.y-(x-f.x)]=1;
}
if(f.y<x-f.x&&vis[f.x+f.y][0]==0)//y->x
{
q.push((state){f.x+f.y,0,f.step+1});
vis[f.x+f.y][0]=1;
}
}
return -1;
} int main()
{
scanf("%d%d%d",&x,&y,&target);
int ans=bfs();
if(ans!=-1) printf("%d",ans);
else printf("impossible");
return 0;
}

CodeVS 1226 倒水问题【DFS/BFS】的更多相关文章

  1. codevs 1226 倒水问题

    1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x, ...

  2. 洛谷P1432 倒水问题(CODEVS.1226)

    To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...

  3. 广度优先搜索 cdoevs 1226 倒水问题

    cdoevs 1226 倒水问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升 ...

  4. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  5. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  6. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  9. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

随机推荐

  1. [2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania

    [2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见&quo ...

  2. [51nod1503]猪和回文 DP

    ---题面--- 题解: 首先观察到题目要求的是合法回文串的个数,而回文串要求从前往后和从后往前是一样的,因此我们假设有两只猪,分别从左上和右下开始走,走相同的步数最后相遇,那么它们走的路能拼在一起构 ...

  3. java一个接口可以继承另外一个接口吗

    一个接口可以继承多个接口. interface C extends A, B {}是可以的. 一个类可以实现多个接口: class D implements A,B,C{} 但是一个类只能继承一个类, ...

  4. JS让任意图片垂直水平居中且页面不滚动

    说一下以前遇到的一个问题: 假设有一张小图,要实现点击查看大图的功能,而这个图的宽高可能会超过浏览器的宽高,这时候我们通过JS来改变图片的宽高,从而实现图片在浏览器居中显示且不滚屏. 方法如下: 首先 ...

  5. [cdoj 1344]树状数组区间加等差数列

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1344 区间加等差数列本质上就是区间修改区间查询,本来想用线段树做,结果这个题就是卡空间和时间……不得已学了区 ...

  6. 迅雷Bolt图像拉伸不清晰的解决办法

    迅雷Bolt库中的图像拉伸的效果锯齿比较严重,常见的导致锯齿的情况: 1.在使用ImageObject时,drawmode为1拉伸模式下: 2.使用Bitmap类的Stretch函数拉伸图像: 虽然I ...

  7. JS alert()、confirm()、prompt()的区别

    这三个都是属于弹框类型的 使用警告.提示和确认消息框来获得用户的输入.这些消息框是 window 对象的接口方法.由于 window 对象位于对象层次的顶层,因此实际应用中不必使用这些消息框的全名(例 ...

  8. (转)HTTP请求中URL地址的编码和解码

    HTTP请求中,类似   http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fv3Jump.html  的地址 如何解码成    http://www ...

  9. abstract 与interface区别

    1.abstract用于修饰类,interface用于修饰接口 2.抽象类中可以有抽象和非抽象方法,接口中只能定义抽象方法,不能有实现 3.抽象类必须被继承,interface被实现 4.抽象类有构造 ...

  10. 前端导出文件功能document.execCommand命令

    参照 http://blog.csdn.net/woshinia/article/details/18664903