[ZOJ 1005] Jugs (dfs倒水问题)
题目链接: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倒水问题)的更多相关文章
- ZOJ 1005 Jugs(BFS)
Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...
- ZOJ 1005 Jugs
原题链接 题目大意:有一大一小两个杯子,相互倒水,直到其中一个杯子里剩下特定体积的水.描述这个过程. 解法:因为两个杯子的容积互质,所以只要用小杯子不断往大杯子倒水,大杯子灌满后就清空,大杯子里迟早会 ...
- ZOJ 1005:Jugs(思维)
Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In the movie "Die Har ...
- A - Jugs ZOJ - 1005 (模拟)
题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...
- ZOJ - 4045District Division dfs划分子树
ZOJ - 4045District Division 题目大意:给你n个节点的树,然后让你划分这棵数使得,每一块都恰好k个节点并且两两间是连通的,也就是划分成n/k个连通集,如果可以输出YES,并输 ...
- [ZOJ 1011] NTA (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...
- 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 ...
- 1005 Jugs
辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. #include<stdio.h> int main(){ int A,B,T,sA,sB; ){ sA=sB=; ){ ){ pr ...
- Jugs(回溯法)
ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In ...
随机推荐
- asp.net中virtual和abstract的区别分析
这篇文章主要介绍了asp.net中virtual和abstract的区别,较为详细的分析了virtual与abstract的概念与具体用法,并以实例的形式予以总结归纳,需要的朋友可以参考下 本文实例分 ...
- SQL开发利器SQL Prompt
SQL开发利器SQL Prompt 5.1完全破解+使用教程 - keepfool - 博客园 http://www.cnblogs.com/keepfool/archive/2012/05/27/2 ...
- Cordova调用Activity
首先需要编写一个CallActivityPlugin插件,专门调用Activity package com.example.plugin; import org.apache.cordova.api. ...
- ios9 URL Schemes列为白名单,才可正常检查其他应用是否安装
http://my.oschina.net/u/1244672/blog/512210 如果你在模拟器上运行可以能还会有以下报错: -canOpenURL: failed for URL: “weix ...
- JSP的Servlet监听器
JSP的Servlet监听器 来源: http://blog.csdn.net/phoenix_17th/article/details/3868670 Servlet 监听器用于监听一些重要事件的发 ...
- MySQL加载并执行SQL脚本文件
第一种方法: 命令行下(未连接数据库) ,输入 mysql -h localhost -u root -p123456 < C:\db.sql 第二种方法: 命令行下(已连接数据库,此时的提示符 ...
- WINDOWS黑客基础(6):查看文件里面的导入表
int main(void) { HANDLE hFile = CreateFile("D:\\Shipyard.exe", GENERIC_READ, FILE_SHARE_RE ...
- 解读Nodejs多核处理模块cluster
来源: http://blog.fens.me/nodejs-core-cluster/ 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发. ...
- perl中的grep函数介绍
grep函数 (如果你是个的新手,你可以先跳过下面的两段,直接到 Grep vs.loops 样例这一部分,放心,在后面你还会遇到它) <pre>grep BLOCK LISTgrep E ...
- 程序包com.sun.image.codec.jpeg不存在 问题的完美解决
原文地址:http://my.oschina.net/zb0423/blog/86507 在使用Hudson进行打包的过程中,因为我们使用了一个pdf文件产生缩略图的功能,倒置添加的源码文件在mave ...