题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5

题目大意:给你6种操作,3个数a,b,n,代表我有两个杯子,第一个杯子的容量是a,第二个杯子容量是b,问能否通过6种操作,使得第二个杯子里装水量为n

dfs搜搜搜!!

状态dfs(x,y) 代表第一个杯子里有水量x,第二个杯子里有水量y。

代码:

 #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int a,b,n;
bool vis[][];
VI ans;
bool flag; void dfs(int x,int y){
// printf("[dfs]:x=%d,y=%d\n",x,y);
if( x<||y< ) return;
if( x>a||y>b ) return;
if( flag ) return;
if( vis[x][y] ) return;
vis[x][y] = true;
if( y==n ){
for(int i=;i<ans.SZ;i++){
if( ans[i]== ) puts("fill A");
else if( ans[i]== ) puts("fill B");
else if( ans[i]== ) puts("empty A");
else if( ans[i]== ) puts("empty B");
else if( ans[i]== ) puts("pour A B");
else if( ans[i]== ) puts("pour B A");
}
puts("success");
flag = true;
return;
}
for(int i=;i<=;i++){
ans.PB(i);
if( i== ) dfs(a,y);
else if( i== ) dfs(x,b);
else if( i== ) dfs(,y);
else if( i== ) dfs(x,);
else if( i== ){
int rb = b - y;
int pa = min(rb,x);
dfs(x-pa,y+pa);
}
else if( i== ){
int ra = a - x;
int pb = min(ra,y);
dfs(x+pb,y-pb);
}
ans.pop_back();
}
vis[x][y] = false;
} int main(){
while(scanf("%d%d%d",&a,&b,&n)!=EOF){
ZERO(vis);
flag = false;
dfs(,);
}
return ;
}

[ZOJ 1005] Jugs (dfs倒水问题)的更多相关文章

  1. ZOJ 1005 Jugs(BFS)

    Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...

  2. ZOJ 1005 Jugs

    原题链接 题目大意:有一大一小两个杯子,相互倒水,直到其中一个杯子里剩下特定体积的水.描述这个过程. 解法:因为两个杯子的容积互质,所以只要用小杯子不断往大杯子倒水,大杯子灌满后就清空,大杯子里迟早会 ...

  3. ZOJ 1005:Jugs(思维)

    Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Har ...

  4. A - Jugs ZOJ - 1005 (模拟)

    题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...

  5. ZOJ - 4045District Division dfs划分子树

    ZOJ - 4045District Division 题目大意:给你n个节点的树,然后让你划分这棵数使得,每一块都恰好k个节点并且两两间是连通的,也就是划分成n/k个连通集,如果可以输出YES,并输 ...

  6. [ZOJ 1011] NTA (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...

  7. Farm Irrigation ZOJ 2412(DFS连通图)

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  8. 1005 Jugs

    辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. #include<stdio.h> int main(){ int A,B,T,sA,sB; ){ sA=sB=; ){ ){ pr ...

  9. Jugs(回溯法)

    ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In ...

随机推荐

  1. 深入认识JavaScript 中的this指针

    深入认识JavaScript 中的this指针this指针是面向对象程序设计中的一项重要概念,它表示当前运行的对象.在实现对象的方法时,可以使用this指针来获得该对象自身的引用.和传统意义的面向对象 ...

  2. html之内联标签img

    img创建的是被链接图像的占位空间,它不会真正地在网页插入图像. 两个必须属性: src:图像的超链接 alt:图像的替代文本 可选属性: height:高度 width:宽度 ismap:将图像定义 ...

  3. VS 2010 编译安装 boost 库 -(和 jsoncpp 库共存)

    boost库的简单应用很容易,网上有很多资料,但是,如果要json 和 boost 一起使用就会出现这样那样的问题, 有时候提示找不到 “libboost_coroutine-vc100-mt-sgd ...

  4. java找jar包、搜索class类 搜索maven

    sourceforge.net https://github.com/ http://www.findmaven.net/搜索class类 http://mvnrepository.com/

  5. Android Studio--学习系列(1)

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  6. 如何通过SecureCRT FTP上传下载文件

    通过SecureCRT  FTP方式从一台机器下载文件到另一台机器上: [root@TEST144239 ~]# ftp 10.30.1.25 Connected to 10.30.1.25 (10. ...

  7. winform 承载 WCF 注意,可能不是工作在多线程模式下

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMo ...

  8. Java-Thread

    1. 线程的创建和启动 1.1 继承Thread 在run方法里,通过this获取当前线程. 多个线程不能共享实例变量. 1.2 通过实现接口 1.2.1 实现Runable接口 在run方法里,只能 ...

  9. Python之Fabric模块

    Fabric是基于Python实现的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包括:命令执行.文件上传.下载及完整执行日 ...

  10. liveusb-creator

    liveusb-creator The liveusb-creator is a cross-platform tool for easily installing live operating sy ...