题目链接: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. Windows命令行查看文件的MD5

    certutil -hashfile D:\apache-tomcat-7.0.68-windows-x64.zip  MD5certutil -hashfile D:\apache-tomcat-7 ...

  2. Eclipse设置UTF-8

    将Eclipse所有的设置都设置为UTF-8: 1) Window -> Preferences -> General -> Workspace -> Text file en ...

  3. 【jmeter】ANT批量执行Jmeter脚本

    一.环境准备: 1.Jdk1.6或以上:命令行输入:java -version,出现如下提示说明安装成功 2.ANT下载:http://ant.apache.org/bindownload.cgi 命 ...

  4. SPOJ #691. Hotel Floors

    A typical flood-fill algorithm application (BFS). Not very complex, except only 1 tip: instead of se ...

  5. 剑指offer系列28--字符流中第一个不重复的字符

    [题目]请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”.当从该字符流中读出前六个字符“google”时,第一个只出现 ...

  6. 51nod 1336 RMQ逆问题

    RMQ问题是一类区间最值问题,这里给出一个特殊的RMQ问题,初始给定一个n长的排列P,注:n长排列是指有1~n这n个整数构成的一个序列每个整数恰好出现一次.并对这个排列P进行M次查询操作,每次查询形如 ...

  7. Linux中文件描述符fd和文件指针flip的理解

    转自:http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...

  8. (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

    ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect for ...

  9. [Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子

    https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1) 关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题.比如ListView的OnI ...

  10. "this class is not key value coding-compliant for the key ..."问题的解决

    今天出现跟着MJ的思路敲的代码,自己最后运行出现这个 错误,发现是 自己在将属性和相关联的控件连线时出现了 错误.一开始取名时出现了错误,发现线连重复了. 在网上又找到了一些出现该类错误的相关解释: ...