题目链接: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. oracle11g导入dmp文件(根据用户)

    已知:用户名.密码.dmp文件 .(指即将导入dmp文件的用户名和密码) 需求:将该dmp文件导入本地oracle11g中. 步骤: 1.将该dmp文件拷贝到G:\oracle11g\admin\or ...

  2. du 命令,对文件和目录磁盘使用的空间的查看

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  3. jquery 事件绑定以及解绑定

    var targetSelect = $("#@(Perfix)tbData tbody tr select[data-target]"); targetSelect.off(&q ...

  4. runliuv, 安卓查看WIFI密码

    用RE查看data/misc/wifi/wpa_supplicant.conf或者其他文件名以.conf结尾的文件

  5. 提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll

    新版本SQLITE,如果下载Precompiled Binaries版会出现提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll. 下载Prec ...

  6. 从1970年1月1日00:00:00 GMT以来此时间对象表示的毫秒数转化为Datetime

    1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)对时间转换 将Long类型转换为DateTime类型 /// <summary> /// 将L ...

  7. 黄聪:使用WORDPRESS自带AJAX方法

    例如给网站每页logo后面的一句名言,点击“换一条”就会ajax动态加载一条,使用了wordpress的自带ajax方法.下面介绍如何使用wordpress自带ajax方法: 1.在header.ph ...

  8. memcached应用场景(转)

     memcached最吸引人的地方主要在于它的分布式.分布式对于互联网应用来讲,按照用途基本上可划分为三种方式:分布式计算.分布式存储和两者兼而有之.memcached是分布式存储的一种.我们常见的分 ...

  9. netty常用代码

    一. Server public class TimeServer_argu { public void bind(int port) throws InterruptedException { Ev ...

  10. Servlet概述及其生命周期

    Servlet和传统CGI程序相比的优点:   1. 只需要启动一个操作系统进程以及加载一个JVM,大大降低了系统的开销 2. 如果多个请求需要做同样处理的时候,这时只需要加载一个类,这也大大降低了开 ...